Bug Tracker

Opened 10 years ago

Closed 10 years ago

#14385 closed bug (notabug)

The data() method returns String values for custom data-* attributes which contain Number values

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: data Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:

Description

The data() method of jQuery returns String values for custom data-* attributes which contain Number values (containing trailing zeroes): http://jsfiddle.net/3A5tp/ (see console or html output).

Tested with the latest jQuery versions 1.x and 2.x in the most recent versions of Chrome and Firefox.

Greets

Change History (1)

comment:1 Changed 10 years ago by gibson042

Component: unfileddata
Resolution: notabug
Status: newclosed

This is by design:

A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.

You can use parseFloat or numeric cast (e.g., +( $el.data("safe") )) to convert values yourself.

Note: See TracTickets for help on using tickets.