Bug Tracker

Modify

Ticket #10174 (closed bug: invalid)

Opened 3 years ago

Last modified 22 months ago

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:1 Changed 3 years ago by jigfox@…

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

comment:2 follow-up: ↓ 3 Changed 3 years 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 3 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 3 years ago by timmywil

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 3 years ago by timmywil (previous) (diff)

comment:5 Changed 2 years ago by rwaldron

#11178 is a duplicate of this ticket.

comment:6 Changed 2 years ago by dmethvin

#11297 is a duplicate of this ticket.

comment:7 Changed 2 years ago by rwaldron

#11805 is a duplicate of this ticket.

comment:8 Changed 22 months ago by rwaldron

#11935 is a duplicate of this ticket.

comment:9 Changed 22 months ago by rwaldron

#11937 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.