Skip to main content

Bug Tracker

Side navigation

#10266 closed bug (wontfix)

Opened September 13, 2011 07:56PM UTC

Closed September 19, 2011 04:07PM UTC

Last modified March 09, 2012 10:14PM UTC

Undefined values passed to $(element).data(key,value) break chaining

Reported by: jsoverson@gmail.com Owned by:
Priority: low Milestone: None
Component: data Version: 1.6.4rc1
Keywords: Cc:
Blocked by: Blocking:
Description

Example here:

The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain.

This issue is also what likely triggered ticket but that ticket was closed prematurely due to the inability to reproduce.

Attachments (0)
Change History (7)

Changed September 13, 2011 08:52PM UTC by james_padolsey comment:1

jQuery.fn.data appears to check for the presence of the value argument by the expression value !== undefined. Another way would be to check the arguments.length, but if that was done then $(el).data('a', undefined) would appear to set a to undefined, but it doesn't (jQuery.data also uses the arg === undefined test). So, if a was already set to 123, it'll remain that way. IMO, this'd be misleading. The question is: should passing an argument, even if it's undefined, still be construed as intentionally passing that argument, or alternatively: willfully accessing the getter functionality of jQuery.fn.data??

What should jQuery(el).data('foo', undefined) be expected to do?

Anyway, like I said, a fix would be easy enough -- checking for the appropriate argument-number in arguments.

Changed September 13, 2011 09:02PM UTC by anonymous comment:2

I think setting a value to undefined is a valid use case, but that might be a different discussion.

I think, regardless, by sending an argument in place of the "value" field, retrieving the previously set value is the least expected result.

I threw in the fix and test case here since I was already working in the code:

https://github.com/jquery/jquery/pull/501

Changed September 13, 2011 09:08PM UTC by timmywil comment:3

component: unfileddata
priority: undecidedlow

All jQuery getters/setters check for undefined. Retrieving an item specifically set to undefined and trying to retrieve one that doesn't exist will produce exactly the same result, so there shouldn't be a reason to set data to undefined. I say use removeData.

Changed September 14, 2011 12:53AM UTC by rwaldron comment:4

description: Example here: \ http://jsfiddle.net/jsoverson/E25eF/4/ \ \ The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain. \ \ This issue is also what likely triggered ticket http://bugs.jquery.com/ticket/9977 but that ticket was closed prematurely due to the inability to reproduce. \ \ Example here: \ \ \ The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain. \ \ This issue is also what likely triggered ticket but that ticket was closed prematurely due to the inability to reproduce. \ \

-1 This request has been made before for other methods and we've argued the color of the explicit undefined parameter bikeshed plenty of times.

Changed September 19, 2011 04:07PM UTC by rwaldron comment:5

resolution: → wontfix
status: newclosed

Changed February 14, 2012 10:17PM UTC by Billie Cleek comment:6

While I understand the decision to not fix this issue, the documentation for .removeData() explicitly says setting value to undefined is one of the preferred ways of ensuring that the data value is not reset to the value of the data- attribute.

Changed February 14, 2012 10:21PM UTC by ajpiano comment:7

This actually seems like it's now fixed as part of #5571 getting fixed. Can you check in the latest edge version please?