Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#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:
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.

Change History (9)

comment:1 Changed 12 years ago by jigfox@…

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

comment:2 Changed 12 years ago by Timmy Willison

Component: unfileddata
Priority: undecidedlow
Resolution: invalid
Status: newclosed

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 12 years 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.''

comment:4 Changed 12 years ago by Timmy Willison

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

Last edited 12 years ago by Timmy Willison (previous) (diff)

comment:5 Changed 11 years ago by Rick Waldron

#11178 is a duplicate of this ticket.

comment:6 Changed 11 years ago by dmethvin

#11297 is a duplicate of this ticket.

comment:7 Changed 11 years ago by Rick Waldron

#11805 is a duplicate of this ticket.

comment:8 Changed 11 years ago by Rick Waldron

#11935 is a duplicate of this ticket.

comment:9 Changed 11 years ago by Rick Waldron

#11937 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.