Don't use the jQuery .data() method. Use .attr() instead.
lookfirst.com
lookfirst.com
var foo = $("body");
foo.data("mykey", "1.410");
foo.data("mykey");
...you get your original "1.410", not 1.41 as the author reports.Secondly, he says there are "several bug reports on the jQuery website, where people are also confused by this behavior and all of them get closed with a wontfix" but links to none of them. Linking to them should be trivial and they'd help his case (or at least make it easy for me to figure out why he sees a bug and I don't). I searched and couldn't find any.
Finally, you shouldn't use .attr() to store arbitrary data as he recommends. Hanging javascript data off of DOM nodes will cause serious memory leaks if you're not careful. That's the reason .data() exists in the first place. See the "Cicular References" section of Understanding and Solving Internet Explorer Leak Patterns: http://msdn.microsoft.com/en-us/library/Bb250448
Think of it this way, say you have a template that is putting some data into an element like this:
<button id="fooButton" data-key="1.400">Click me to edit something</button>
Now, using .data() to select the information out, will always return 1.4 instead of what you want, which is 1.400.
Edit, after the parent was edited to include the data attribute example:
You should update your post to clarify that you're talking about data attributes. That's a significant detail and shouldn't have been left out.
Now that you've clarified, jQuery does behave as you've stated, but it's not a bug. I can see how it could be unexpected behavior to some, but it's explicitly stated in the .data() documentation:
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.
http://api.jquery.com/data/#data2A string of "1.410" is not the same as a float of 1.41. Running "1.410" full-circle through data() corrupts the input string. When you have data that has been entered by a user, you can't guarantee that it's not something that will happen to autoconvert.
This behavior is totally broken and should never be used. It will work 99.95% of the time, then fail in bizarre ways when a user types in something magic. These are the worst bugs in the world to hunt down. This really shouldn't be in jquery.
I can see why they would do this. Having to parseInt all default values wouldn't be very jQuery-like.
1.410 === 1.41
<div id="test" data-foo="1.1">
$('#test').data('bar', '1.1');
typeof $('#test').data('foo'); // number
typeof $('#test').data('bar'); // string
If the data-foo value is user input, then converting it to a number might not be expected. E.g. <div data-emoticon="0.0" /> elem.data('foo', { 'a':1, 'b':2 });
elem.data('foo'); // Returns the object
Is certainly more desirable than: elem.attr('foo', { 'a':1, 'b':2 });
elem.attr('foo'); // Returns "[object Object]"If he wanted to store a string, then he should actually store a string. .data() is just a thin wrapper around getting and setting values on an object, which initialises its initial values from the data-* attributes on the element in question (which should always be strings).
I don't understand the issue. He is putting in 1.410 and gets 1.41? Isn't that the expected result?
<div id="test" data-foo="bar"></div>
<script>
$("#test").removeData('foo');
console.log("#test").data('foo'));
//bar
</script>
The thing is removeData is pretty much useless because it only removes data from the jQuery cache, but if the data is missing from the cache jQuery will look in the DOM anyway.The root issue here is that the developer assumed how somethig worked, and didnt bother to learn how it actually worked.
I actually do have issues with data attribute for this exact reason, but that is no excuse for not looking up how it actually works.
localStorage['a'] = {a:1}; localStorage['a'] -> "[object Object]"
Crap.