I have a div with an attribute data-myval = "10"
. I want to update its value; wouldn't it change if I use div.data('myval',20)
? Do I need to use div.attr('data-myval','20')
only?
Am I getting confused between HTML5 and jQuery? Please advise. Thanks!
EDIT: Updated div.data('myval')=20
to div.data('myval',20)
, but still the HTML is not updating.
div
? A jQuery object or element? – Graybearddiv.data('myval')=20
wouldn't work to store a value only because the syntax is wrong - see the answers for the correct syntax. But note that.data()
doesn't actually update the element attribute, it stores the data elsewhere. – Fluorenedata()
didn't work as expected (for the nth time in my career), I finally decided to get to the bottom of it. I documented my findings in my own answer, but to summarize,data
probably doesn't work the way you expect.attr
probably does work the way you expect. Useattr
. – Fala