[jQuery] Integers can be rounded with .data() so use .attr() to retrieve them

Tadashi Shigeoka ·  Thu, June 12, 2014

When using jQuery’s .data() method, integers with many digits can sometimes be rounded, so let’s use .attr() to retrieve them instead.

jQuery

With jQuery 1.7.2



Click

> //JavaScript
$('#link').data('id')
1.2345678901234568e+29

$('#link').attr('data-id')
"123456789012345678901234567890"

With jQuery 1.11.1



Click

> //JavaScript
$('#link').data('id')
"123456789012345678901234567890"

$('#link').attr('data-id')
"123456789012345678901234567890"

That’s all from the Gemba.

Reference Information