#8288 closed bug (wontfix)
.data(key) no longer returning null?
Reported by: | 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.
Change History (6)
comment:1 Changed 12 years ago by
Component: | unfiled → data |
---|---|
Priority: | undecided → high |
Status: | new → open |
comment:2 Changed 12 years ago by
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
Resolution: | → wontfix |
---|---|
Status: | open → closed |
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
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:6 Changed 12 years ago by
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.
Confirmed
1.4.2 http://jsfiddle.net/rwaldron/pHTP4/1/
1.5.0 http://jsfiddle.net/rwaldron/pHTP4/2/