5

I have just encountered some very disturbing behavior in jQuery 1.6.2 that I hope someone can explain. Given the following markup...

<div id="test" data-test="    01">Test</div>

Can someone tell me why accessing the attribute through .data() causes it to be parsed to an int?

var t = $("#test").data("test");
alert(t);  // shows "1"

t = $("#test").attr("data-test");
alert(t);  // shows "    01"

Of course I have proof on jsFiddle of this behavior.

1
  • 1
    Thank you, Josh for the explanation. I've removed my answer, but I will post this point as a comment. Setting the data attribute with .data on the client side retains a string format. (this of course doesn't help in the case of server side setting of the data-test attribute.) Commented Sep 30, 2011 at 18:50

3 Answers 3

7

From the doc for data(key):

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.

Since your example string CAN be converted to a number, it is.

Edit: As Joseph points out, this only applies when reading data straight from the element, like you're doing in this example. If you could set it first (i.e. data(key,value) before reading), then the behavior disappears. The getter, when reading from the actual DOM element, performs type coercion.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, I got to say this is some obtrusive (almost criminal) behavior!
This is not entirely correct. .data can store strings that can be converted to a number. See my answer below.
@Joseph Yes, data can, but not the way this example uses it. I'll edit to clarify.
0

jQuery people say:

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.

Every attempt is made to convert the string (html5 data attribute value) 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.

From this page: http://api.jquery.com/data/

Comments

0

To add to Paul Equis's answer,

If you want to access it as a string, use the .attr() method.

var t = $("#test").attr("data-test");

Edit: here's a demonstration showing one of the benefits of jQuery interpreting the data attribute.

http://jsfiddle.net/FzmWa/1/

2 Comments

Yes, that was in the question
Ah, sorry I missed it. The conversion is intended so that you can do things such as passing in arrays or json into the data attribute then accessing it as such when you access the data using .data().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.