Side navigation
#13785 closed bug (fixed)
Opened April 16, 2013 08:45PM UTC
Closed April 16, 2013 10:13PM UTC
Last modified May 29, 2013 02:49PM UTC
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: | rwaldron |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.9.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
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!
Attachments (0)
Change History (4)
Changed April 16, 2013 08:46PM UTC by comment:1
owner: | → rwaldron |
---|---|
status: | new → assigned |
Changed April 16, 2013 09:57PM UTC by comment:2
This is definitely a bug. jQuery 2.x correctly returns undefined.
Changed April 16, 2013 10:13PM UTC by comment:3
resolution: | → fixed |
---|---|
status: | assigned → closed |
Looks like this is fix in the latest 1.x-master
Changed May 29, 2013 02:49PM UTC by comment:4
After jQuery 1.10 was released, I am still seeing this bug occur. I've put another jsFiddle here.