For this html
this js:
console.log($(\'div\').data(\'a\'));
retur
Yep, use:
console.log($('div').attr('data-a'));
From the jQuery doco on .data():
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.
So you need to say:
console.log($('div').attr('data-a'));
(Note that you specify the full attribute name, including the 'data-' prefix.)
You can bypass data()
and grab the attribute directly:
console.log($('div').attr('data-a'));
Demo