Skip to main content

Bug Tracker

Side navigation

#8288 closed bug (wontfix)

Opened February 15, 2011 09:44PM UTC

Closed February 15, 2011 11:19PM UTC

Last modified May 08, 2011 06:18AM UTC

.data(key) no longer returning null?

Reported by: contact@alanhogan.com Owned by:
Priority: high Milestone: 1.next
Component: data Version: 1.5
Keywords: Cc:
Blocked by: Blocking:
Description

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.

Attachments (0)
Change History (6)

Changed February 15, 2011 09:50PM UTC by rwaldron comment:1

_comment0: Confirmed \ \ http://jsfiddle.net/rwaldron/pHTP4/1/1297806636513271
component: unfileddata
priority: undecidedhigh
status: newopen

Changed February 15, 2011 10:21PM UTC by rwaldron comment:2

keywords: → needsdocs

Update docs to read:

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

Code should be updated accordingly

Changed February 15, 2011 11:19PM UTC by jitter comment:3

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.

Changed February 15, 2011 11:57PM UTC by contact@alanhogan.com comment:4

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?

Changed February 16, 2011 12:01AM UTC by anonymous comment:5

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

http://jsfiddle.net/AmbxU/

http://jsfiddle.net/g4GrZ/

Changed May 08, 2011 06:18AM UTC by addyosmani comment:6

keywords: needsdocs

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