For myself, using Jquery lib 2.1.1, the following did NOT work as I expected:
Element element data attribute value:
$('.my-class').data('num', 'myValue'); console.log($('#myElem').data('num');
BUT the element itself remains without an attribute:
<div class="my-class"></div>
I need to update the DOM so that later $ ('. My-class [data-num = "myValue"]') // the current length is 0
Therefore I had to do
$('.my-class').attr('data-num', 'myValue');
To update the DOM:
<div class="my-class" data-num="myValue"></div>
Whether the attribute will exist or $ .attr will not be overwritten.
Brian Ogden Nov 23 '14 at 23:05 2014-11-23 23:05
source share