Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#8288 closed bug (wontfix)

.data(key) no longer returning null?

Reported by: [email protected] Owned by:
Priority: high Milestone: 1.next
Component: data Version: 1.5
Keywords: Cc:
Blocked by: Blocking:


I just upgraded to jQuery 1.5.0 from 1.4.2, I think it was. I previously had code that relied upon this documented behavior of .data(key).

From http://api.jquery.com/data/#data2 — “If nothing was set on that element, null is returned.”

Thus, I could do:

if($('#foo').data('my_key') === null) {
  // calculate & set it… 

In jQuery 1.5.0, this seems to be returning 'undefined' instead.

Change History (6)

comment:1 Changed 12 years ago by Rick Waldron

Component: unfileddata
Priority: undecidedhigh
Status: newopen
Last edited 12 years ago by Rick Waldron (previous) (diff)

comment:2 Changed 12 years ago by Rick Waldron

Keywords: needsdocs added

Update docs to read:

If nothing was set on that element, undefined is returned.

Code should be updated accordingly

comment:3 Changed 12 years ago by jitter

Resolution: wontfix
Status: openclosed

This won't be fixed. .data() only returned null in some very specific circumstances. And the behavior didn't make much sense the way it was.

var x = $("elem1");
x.data("foo"); // null

var y = $("elem2");
y.data(); // {}
y.data("foo"); // undefined

var z = $("elem3");
z.data("bar", "tender");
z.data("foo"); //undefined

So null was only returned if there was no data at all on the element. But it returned undefined if there was already some other data.

comment:4 Changed 12 years ago by [email protected]

1) I’m not seeing the change to the docs. Is there a delay?

2) Okay, I’m all for consistency. Shouldn’t this be noted somewhere, though? Added to the changelog perhaps?

comment:5 Changed 12 years ago by anonymous

Hmm, seems the change was actually between 1.4.2 and 1.4.4.

http://jsfiddle.net/AmbxU/ http://jsfiddle.net/g4GrZ/

comment:6 Changed 12 years ago by addyosmani

Keywords: needsdocs removed

Updated needsdocs: http://api.jquery.com/data/. Now includes changed verbiage to correct what is now being returned as well as a minor example.

Note: See TracTickets for help on using tickets.