28

Check out this simple example on jsfiddle

<div id ="a" data-siteid="00005">00005 turns into:</div>
<div id="b" data-siteid="S00005">S00005 turns into: </div>

code

$('#a').append($('#a').data("siteid"));
$('#b').append($('#b').data("siteid"));

​ result

00005 turns into:5
S00005 turns into: S00005

I would like to return "00005" and "S00005".

2
  • Sorry @FelixKling, it's a string, but not the string I needed. My siteid is actual the string '00005' Commented Apr 24, 2012 at 11:35
  • Yeah... I understood your question, deleted my comment. A bit more explanation would have helper though ;) For example, that S00005 is used to force returning a string and that this should show that 00005 is converted to a number. Commented Apr 24, 2012 at 11:35

4 Answers 4

42

Try

$('#a').append($('#a').attr('data-siteid'));
$('#b').append($('#b').attr('data-siteid'));

From the jQuery Docs

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.

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

2 Comments

Thanks a lot for also providing the doc!
The docs clearly say that attr() returns a string but simple code like element.attr('data-text', '12'); text = element.attr('data-text'); returns a number for me. What am I doing wrong?
5

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.

From here: http://api.jquery.com/data/#data-html5

1 Comment

I think @binarious was faster :)
0

enter image description here

JQuery returns integer if you have a value as a string, i.e. "one integer value", i.e. "100", if it is not, i.e. "0, 3" then it returns it as string. Beware, JQuery's data() function doesn't always return what you expect, check the type and process it.I hope this helps too.

if (typeof( $("xyz").data() ) == 'number') { 
   do Something 
} 
else { 
   do Something else 
}

Comments

-2

I know this is a slightly older post, but another way you can approach this is to convert the data attribute value to a string:

$('#a').data("siteid").toString()

or

$('#a').data().siteid.toString()

Some examples of how this can work:

> (12345).toString()
"12345"

> (14.5).toString()
"14.5"

> (-14.5).toString()
"-14.5"

> "bob".toString()
"bob"

> (true).toString()
"true"

> ({a: "b"}).toString()
"[object Object]"

>(function(){console.log("bob")}).toString()
"function (){console.log("bob")}"

The parenthesis in the example are there to avoid assigning variables, since you cannot directly use #toString on the number directly: 123.toString(), but you can when it assigned to a variable or enclosed in parenthesis: (123).toString().

Just remember that you won't be able to convert null or undefined to a string.

Also something interesting happens with arrays and undefined and null values:

> (["bob", 123, true, null, undefined, this]).toString()
"bob,123,true,,,[object Window]"

2 Comments

It doesn't solve the problem: in OP case the data was 00005. When reading from data it'll become 5 (because of the implicit conversion) and then the toString will make it "5".
Nooooo. Converting to a string may be too late. For example, if the number is 64-bits long, it would have been converted to a float point, losing some resolution. Subsequent conversion back to integer would be wrong, wrong!

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.