Ticket #10174 (closed bug: invalid)
data attributes with large integers don't work
| Reported by: | jigfox@… | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | data | Version: | 1.6.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
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.
Change History
comment:2 follow-up: ↓ 3 Changed 21 months ago by timmywil
- Priority changed from undecided to low
- Resolution set to invalid
- Status changed from new to closed
- Component changed from unfiled to data
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 +/- 253, which is not large enough for the numbers you are using.
comment:3 in reply to: ↑ 2 Changed 21 months ago by jigfox@…
Replying to 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.''
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Forgot the jsfiddle link: http://jsfiddle.net/nn3ek/