For this html
<div data-a="0123"></div>
this js:
console.log($('div').data('a'));
returns 123
. Is there any way without adding extra-chars and cutting it to get a string 0123
?
Jsfiddle: http://jsfiddle.net/PXw2J/
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With