Search code examples
jqueryjquery-1.4

modify jQuery.data()


Let's say I have a bunch of jQuery's .data() set for an HTML element like so:

$("#my_element").data("myData", {
    data1: myData1,
    data2: myData2,
    data3: myData3
});

Now let's say I want to modify the value of data2 to be myData2Changed. How do I modify just this data for my element without having to rewrite the same code above with all the other data?


Solution

  • $.data stores objects by reference.

    Therefore, you can simply modify the object in-place:

    $(...).data('myData').data2 = whatever;