Re:Don't use the jQuery .data() method. Use .attr() instead.
by underwater on Sunday December 18, 2011 @05:56PM
(#3367772)
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" />
Re:Don't use the jQuery .data() method. Use .attr() instead.
by pxm on Sunday December 18, 2011 @05:45PM
(#3367747)
Yep, latchkey, I think you might want to rethink the article. As others have pointed out, you're seeing JavaScript's behaviour, not jQuery's. And this:
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]"
1.410 === 1.41