Bug Tracker

Ticket #14101 (closed bug: fixed)

Opened 16 months ago

Last modified 9 months ago

Version 1.10 .data() differs from 1.8 when getting data from non-existent object

Reported by: Alex Robbin <agrobbin@…> Owned by: gnarf
Priority: high Milestone: 1.11/2.1
Component: data Version: 1.10.1
Keywords: Cc: gnarf
Blocking: Blocked by:

Description

This is a re-posting of ticket #13785, as it is still a problem in jQuery 1.10 but I am not able to re-open that ticket. I have reposted the information with new jsFiddles below.


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) and jQuery 1.10.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.

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

comment:1 Changed 16 months ago by timmywil

  • Priority changed from undecided to high
  • Status changed from new to open
  • Component changed from unfiled to data
  • Milestone changed from None to 1.10.3/2.0.4

comment:2 Changed 16 months ago by rwaldron

  • Owner set to rwaldron
  • Status changed from open to assigned

comment:3 Changed 16 months ago by rwaldron

  • Cc gnarf added
  • Owner rwaldron deleted
  • Status changed from assigned to open

Actually, I'm going to bail out for this one and ping Corey

comment:4 Changed 14 months ago by dmethvin

  • Milestone changed from 1.10.3/2.0.4 to 1.11/2.1

Bulk update for new milestone

comment:5 Changed 12 months ago by rwaldron

  • Owner set to gnarf
  • Status changed from open to assigned

comment:7 Changed 12 months ago by rwaldron

  • Status changed from assigned to closed
  • Resolution set to fixed

comment:9 Changed 9 months ago by dmethvin

  • Summary changed from JQUERY 1.10'S .DATA() RESULT DIFFERS FROM 1.8 WHEN ATTEMPTING TO GET DATA FROM A NON-EXISTENT OBJECT. to Version 1.10 .data() differs from 1.8 when getting data from non-existent object
Note: See TracTickets for help on using tickets.