
Don't use the jQuery .data() method. Use .attr() instead. - latchkey
http://lookfirst.com/2011/12/dont-use-jquery-data-method-use-attr.html
======
unfletch
As far as I can tell this might just be false. If you go to github.com (a
random site which happens to include query 1.7.1 -- the latest version), open
the console in either firebug or webkit inspector, and run the author's
example:

    
    
        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>

~~~
latchkey
Sorry, I shouldn't have had quotes around the 1.410

<http://jsfiddle.net/kgp2H/>

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.

~~~
Flavius
You are not being serious, right?

1.410 === 1.41

~~~
underwater
His example is unclear, but he's complaining about the behavior of
conditionally casting the initial data values based on pattern matching.

    
    
       <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" />

------
glenjamin
In what way does the author think this behaviour is wrong? 1.410 === 1.41

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).

------
jcampbell1
This is bad advice. data() takes care of many details that .attr() does not,
such as preventing IE memory leaks or trampling an existing DOM property.

I don't understand the issue. He is putting in 1.410 and gets 1.41? Isn't that
the expected result?

------
voidr
I hade my gripes with .data too:

    
    
        <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.

------
voidfiles
So, first the article talks about strings, but then does all of it's examples
using floats. We all make mistakes. Hopefully we can help with that.

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.

~~~
stickfigure
The problem is that the way it works is not actually useful. When I have user
input data of 1.400, I need this string preserved as 1.400. I can't have
jQuery mangling the data just because (to it) the string happens to look like
a float.

------
bryanlarsen
The jquery docs say that it stores as Json. 1.4000 in Json is a float. Use
data-key="'1.400'" to get a string.

------
latchkey
Here is another example of what I'm talking about:

<http://jsfiddle.net/KwjvA/>

------
lang
I wasn't aware of this. This looks like a bad design choice indeed. I hate
when libraries convert stuff implicitly for you. html5's localStorage has a
similar problem. Only, it converts whatever you are trying to store into a
string. Would be better if it accepted only strings and threw an error if you
tried to store something else, imho.

~~~
stefankendall
Agreed that localStorage should throw errors. This is particularly fun:

localStorage['a'] = {a:1}; localStorage['a'] -> "[object Object]"

Crap.

