20

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?

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207

2 Answers2

35

You can use 'attr':

var number = $(this).find('img').attr("data-number");
Josh
  • 12,448
  • 10
  • 74
  • 118
17

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800