Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#13785 closed bug (fixed)

jQuery 1.9's .data() result differs from 1.8 when attempting to get data from a non-existent object.

Reported by: agrobbin Owned by: Rick Waldron
Priority: undecided Milestone: None
Component: unfiled Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:


I am not sure if this is a change in implementation that was done on purpose, or in error, but I cannot find another ticket about it so I wanted to double-check.

In jQuery 1.8.3 (jsFiddle here), when you have attempted to retrieve a data key from an element that doesn't actually exist on the page, the result from data() was "undefined". In jQuery 1.9.1 (jsFiddle here), that result is now "null". I have just had the jsFiddles do an alert showing what the value is when attempting to call .data() on an element that doesn't exist.

Normally, I wouldn't have ever noticed this change since both null and undefined are false-y, but the use case I have run in to, is when doing something like this. When gathering some data values from an element in order to pass them along to a subsequent POST request, I am building a basic javascript object that I then pass to $.post(). In jQuery 1.8.3, if the element did not exist (which can definitely happen on occasion), the params looked like this:

{id: undefined, type: undefined}

When $.post() then took that object to parameterize it, no parameters are actually POSTed to the /test/endpoint. However, in jQuery 1.9.1, the params look like this:

{id: null, type: null}

Because null values aren't stripped out/ignored when doing a $.post() like undefined values are, two empty values are POSTed, which our back-end system is not expecting.

Interestingly, when you call .data() on an element that does exist but has no value for the supplied data key, the return value is undefined in jQuery 1.9.1. It is only when the element doesn't actually exist on the page that the value has changed between 1.8.3 and 1.9.1.

Again, I am not sure if this is done purposefully or not, but in the gist that I have linked to, you'll see that the implementation does actually change (and jQuery migrate does not mitigate it).

I hope I have been clear in explaining the change, and any insight you guys can give me would be greatly appreciated. Thanks!

Change History (4)

comment:1 Changed 10 years ago by Rick Waldron

Owner: set to Rick Waldron
Status: newassigned

comment:2 Changed 10 years ago by Rick Waldron

This is definitely a bug. jQuery 2.x correctly returns undefined.

comment:3 Changed 10 years ago by Rick Waldron

Resolution: fixed
Status: assignedclosed

Looks like this is fix in the latest 1.x-master

comment:4 Changed 10 years ago by Alex Robbin <[email protected]…>

After jQuery 1.10 was released, I am still seeing this bug occur. I've put another jsFiddle here.

Note: See TracTickets for help on using tickets.