Maen
Maen

Reputation: 10698

HTML5 data DOM's change isn't seen by JQuery

I'm using HTML5's data to store some informations on a div on my website, and whenever I change the value of this data with jQuery, the DOM's updating well, but jQuery doesn't see the update when I try to retrieve the value.

I update the value of this data with $('#myDiv').attr('data-my_data',value);, and get the value back with $('#myDiv').data('data-my_data').

Here's the fiddle to illustrate my problem.

Is this happening because of some jQuery's initial representation of the DOM that doesn't update?

I don't get it, any help will be appreciated!

Configuration : Chrome 25 - jQuery 1.9.0 - Mac OSx 10.7

Upvotes: 0

Views: 1150

Answers (2)

Ravi Gadag
Ravi Gadag

Reputation: 15881

you can set the values to the data('yourKey','Yourvalue')

$(yourElement).data('isEdit','1');

according to your Example

$('#myDiv').data('data-my_data','value');

Attr : it will add attributges to the dom object.

prop : it will add properites in memory. so your data will be stroed in dataSet of DOM.

for more info check this attr vs prop

Upvotes: 1

Ram
Ram

Reputation: 144719

data-* attributes are mapped to dataset DOMString object, use data method as setter, do not use attr for properties.

$('#testDiv').data('test_data', newData).html("My data is : " + newData);

http://jsfiddle.net/zgKdg/

Upvotes: 1

Related Questions