I’m attempting to grab a data-* with jQuery. My problem is that jQuery reads my string of numbers as a number and as such drops the leading zero.
HTML
<tr data-string-number="0123456789">... (website layout, jk) ...</tr>
jQuery 1.7.2
var string_number = $('#selector').data('string-number');
// string_number == 123456789
// string_number != '0123456789'
Seems simple enough however this always drops the leading zero.
data-string-number is always going to be a number and may or may not have a leading zero. Currently it has a standard length but I can’t say at this point if that will stay true.
Current only thought is to prefix it with a non-numeric and remove it straight away. This feels hacky and makes me sad.
Any thought appreciated.
Thanks.
Use this:
The
.data()method does data conversion by design. The.attr()method simply returns the attribute as is (as a string). Note that when using.attr()you need to supply the full name of the attribute including the"data-"prefix.