Ticket #7837 (closed bug: worksforme)
$.data don't behave the same as $.fn.data with data-* attributes
|Reported by:||Pomeh <pomeh13@…>||Owned by:||Pomeh <pomeh13@…>|
The bug is about jQuery.data and jQuery.fn.data.
I ran tests with jQuery version 1.4.4, on Windows Vista (please don't blame me!) and on following browsers:
- Internet Explorer Beta 9.0.7960 (with all document modes 9, 8 and 7)
- Chrome 8.0.552
- Firefox 3.6.13
I ran the same tests, except for IE 9, on Windows XP. All tests confirm the unexpected behavior.
I've create a test case here: http://jsfiddle.net/pomeh/bJ6Nh/
If we have a DOM element "myDomElement" which has a "data-state" attribute and its value "my-state-value", a call to jQuery.data(myDomElement, 'state') returns "undefined" instead of "my-state-value".
But a call to jQuery(myDomElement).data('state') returns the right value ("my-state-value").
Also, as shown in the test case, if we first call to jQuery(myDomElement).data('state') and then we try jQuery.data(myDomElement, 'state'), this time the value will be correct.
I think the reason of the bug is simple:
- first, jQuery.data doesn't get data-* values because the call to internal "dataAttr" method occurs only in the jQuery.fn.data method (see https://github.com/jquery/jquery/blob/master/src/data.js#L158)
- second, why two successive calls to jQuery.fn.data and jQuery.data fixes the issue is because the internal "dataAttr" method store the value with a call to jQuery.data (see https://github.com/jquery/jquery/blob/master/src/data.js#L224)
- Owner set to Pomeh <pomeh13@…>
- Status changed from new to pending
- Status changed from pending to new
- Keywords needsreview added
- Priority changed from undecided to high
- Status changed from new to open
- Component changed from unfiled to data