The jQuery.attr and .data method give a different value for one attribute

Here is my html code

<li class="current" title="some title" data-atype="some type" data-aid="119697371285186601"> Some text </li> 

This is what I get from the firebug / google chrome debugger console,

 >> $().jquery "1.7.2" >> ($('li.current:first')).data('aid') 119697371285186610 >> ($('li.current:first')).attr('data-aid') "119697371285186601" 

I was looking for a problem and I could not find the exact cause and solution of the problem. If someone could help me find a solution and the cause of the problem, that would be very helpful.

Thanks in advance...

+4
source share
2 answers

$.fn.data tries to be smart about number types and converts them to integers. However, integers in JavaScript are nothing more than pop-ups and therefore become less accurate as they get closer to larger values. Id adheres to attr() since it will always return a string. There is a ticket for this, but its marked "WONTFIX". For more information on the limitations of large numbers in JavaScript, see this article .

+4
source
 <ul> <li class="current" title="some title" data-atype="some type" data-rrr="11969737128518660155555"> Some text </li> </ul> <span></span> $(document).ready(function(){ $('span').html('DATA ATTRIBUTE: '+$('li.current:first').data('rrr')+ ' <br /> ATTRIBUTE: '+ $('li.current:first').attr('data-rrr')); }); Output / result: Some text DATA ATTRIBUTE: 1.1969737128518661e+22 ATTRIBUTE: 11969737128518660155555 

http://jsfiddle.net/nanoquantumtech/usydd/#base

// ================================================== ========= //

  <ul> <li class="current" title="some title" data-atype="some type" data-test1="119697371285186601555" data-test2="11969737128518660155555">Some text </li> </ul> <span></span> $(document).ready(function() { $('span').html('DATA ATTRIBUTE: <br />' + $('li.current:first').data('test1')+ ' , <br /> '+$('li.current:first').data('test2') + '<br /><br /> ATTRIBUTE: <br />' + $('li.current:first').attr('data-test1')+' , <br />'+$('li.current:first').attr('data-test2')); });​ Out Put For jquery version < 1.7.2 Some text DATA ATTRIBUTE: 119697371285186610000 , 1.1969737128518661e+22 ATTRIBUTE: 119697371285186601555 , 11969737128518660155555 

Demo 1

 Out Put For jquery version 1.8.2 Some text DATA ATTRIBUTE: 119697371285186601555 , 11969737128518660155555 ATTRIBUTE: 119697371285186601555 , 11969737128518660155555 

Demo 2

+1
source

All Articles