For the HTML DOM element -
<div style="border:1px solid; width:100px; background:#FFF" class="btn" data-btnNo="1">Button</div>
What is the difference between the two JavaScript lines below?
$(this).attr("data-btnNo");
and
$(this).data("btnNo");
According to my tests on JSFiddle, I see that the first one works, while the second does not. And I am trying to understand 'Why?'
Does jQuery maintain a separate data for each DOM elements? According to the jQuery doc of .data(), I get to understand that .data() gets the value in the HTML5 data-*
into it's own data. Is the reverse true? If I do $(this).data("myData","jkl345");
will it create an HTML5 attribute data-myData="jkl345"
on $(this)
?
I also came across jQuery.data() that seems to extend the .data() to apply data to any DOM element.
Added Later: There seems to be an answer at jQuery Data vs Attr?. The only thing not answered there is does $(this).data("newDataAttri","myVal")
create data-newDataAttri="myVal"
? I am beginning to believe that it does not and only stores it in the DOM node. Can anyone confirm this?