i have this html.
<img src="2.png" alt="Pending" data-number="00991" data-status="0">
i am using the following jQuery to fetch the value.
var number = $(this).find('img').data('number');
above jQuery code fetches values with non-leading zeros, for example the above code will only fetch 991
instead of 00991
, what is going wrong?
You can use 'attr':
var number = $(this).find('img').attr("data-number");
From the fine manual:
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 jQuery thinks that code is a number and drops the leading zeros. Use attr('data-number')
if jQuery is trying to be too smart for your data attributes.
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