Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery data attribute removes leading zeros

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?

like image 423
Ibrahim Azhar Armar Avatar asked Nov 01 '11 04:11

Ibrahim Azhar Armar


2 Answers

You can use 'attr':

var number = $(this).find('img').attr("data-number");
like image 181
Josh Avatar answered Oct 07 '22 00:10

Josh


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.

like image 33
mu is too short Avatar answered Oct 07 '22 01:10

mu is too short