Bug Tracker

Modify

Ticket #7837 (closed bug: worksforme)

Opened 2 years ago

Last modified 2 years ago

$.data don't behave the same as $.fn.data with data-* attributes

Reported by: Pomeh <pomeh13@…> Owned by: Pomeh <pomeh13@…>
Priority: high Milestone: 1.6
Component: data Version: 1.4.4
Keywords: needsreview Cc:
Blocking: Blocked by:

Description

The bug is about jQuery.data and jQuery.fn.data.

Tests

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/

Description

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.

Correction

I think the reason of the bug is simple:

Change History

comment:1 follow-up: ↓ 2 Changed 2 years ago by dmethvin

  • Owner set to Pomeh <pomeh13@…>
  • Status changed from new to pending

It's intentional that jQuery.data does not pull data- attributes; only the jQuery.fn.data method does that. Was there some documentation you saw that indicated otherwise?

comment:2 in reply to: ↑ 1 Changed 2 years ago by Pomeh <pomeh13@…>

  • Status changed from pending to new

Replying to dmethvin:

It's intentional that jQuery.data does not pull data- attributes; only the jQuery.fn.data method does that. Was there some documentation you saw that indicated otherwise?

No there wasn't, but I was expected they works the same way, that's ok maybe I'm wrong I'm sorry.

But if they don't and won't works the same way, there's still a problem. Looks at this:  http://jsbin.com/ahicu4/edit (sorry about JS Bin but jsFiddle is down right now). The problem is that when you call first $.fn.data *then* $.data on the same element, the $.data return the data-* attributes !

That's because $.fn.data pull those attributes and store them into the internal cache, then $.data read that cache, so the value is retrieved.

So the way you call $.data may alter the value being retrieved.

comment:3 Changed 2 years ago by snover

  • Keywords needsreview added
  • Priority changed from undecided to high
  • Status changed from new to open
  • Component changed from unfiled to data

comment:4 Changed 2 years ago by john

  • Status changed from open to closed
  • Resolution set to worksforme

Yeah, this is the intended behavior. As a end-user you should be interacting directly with jQuery.fn.data.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.