Side navigation
#10174 closed bug (invalid)
Opened August 31, 2011 07:08AM UTC
Closed August 31, 2011 01:50PM UTC
Last modified June 19, 2012 09:12PM UTC
data attributes with large integers don't work
Reported by: | jigfox@me.com | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | data | Version: | 1.6.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
If I have some divs like this:
<div data-some="401923275274663446"></div> <div data-some="123456789012345678"></div>
$('div:first').data('some'); $('div:last').data('some');
will return 401923275274663400
and 123456789012345680
.
But If I use $('div').attr('data-some')
I get the expected value. I have found out, that this is a problem in javascript's parseInt
method. Perhaps jQuery should return the data always as String so the user can decide what he wants to do with the data. Or at least check the size first and than decide depending on the size if you want to parse it as Integer or Float.
Attachments (0)
Change History (9)
Changed August 31, 2011 07:11AM UTC by comment:1
Changed August 31, 2011 01:50PM UTC by comment:2
component: | unfiled → data |
---|---|
priority: | undecided → low |
resolution: | → invalid |
status: | new → closed |
First, this is a feature and is described in http://api.jquery.com/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." No need for two functions to give you exactly the same thing.
As for determining Integer or Float, that is not possible. Integer and Float types do not exist in javascript. They are both the more general type, Number (which is always floating-point). There is fixed point and exponential notation, but the range is always the same. For precision numbers in javascript, the range is +/- 2^53, which is not large enough for the numbers you are using.
Changed August 31, 2011 05:31PM UTC by comment:3
Replying to [comment:2 timmywil]:
First, this is a feature and is described in http://api.jquery.com/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." No need for two functions to give you exactly the same thing.
So this is a feature that isn't working, it should be able to see if the number could be to big for converting and than leave it as string as described on http://api.jquery.com/data
As this can't be convertet, it should be kept a string.
!''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__.!''
Changed August 31, 2011 07:30PM UTC by comment:4
_comment0: | It attempts it very well actually. Checking if a number is too big is an edge that wouldn't be worth the code required. It sounds like you just need to use .attr(). → 1314819021618493 |
---|---|
_comment1: | It attempts it very well actually. Checking if a number is too big is an edge case that wouldn't be worth the code required. It sounds like you just need to use .attr(). → 1314819136766867 |
It attempts it very well actually (the result is a number, just not the number you want). Checking if a number is too big is an edge case that wouldn't be worth the code required. It sounds like you just need to use .attr().
Forgot the jsfiddle link: http://jsfiddle.net/nn3ek/