Bug Tracker

Ticket #8288 (closed bug: wontfix)

Opened 4 years ago

Last modified 3 years ago

.data(key) no longer returning null?

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

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.

Change History

comment:1 Changed 4 years ago by rwaldron

  • Priority changed from undecided to high
  • Status changed from new to open
  • Component changed from unfiled to data
Last edited 4 years ago by rwaldron (previous) (diff)

comment:2 Changed 4 years ago by rwaldron

  • 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 4 years ago by jitter

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

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 4 years ago by contact@…

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 4 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 3 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.