Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML data and a string that looks like a number

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/

like image 614
zerkms Avatar asked Dec 09 '22 05:12

zerkms


1 Answers

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.)

like image 117
nnnnnn Avatar answered Dec 28 '22 09:12

nnnnnn