Bug Tracker

Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#6385 closed bug (wontfix)

getOrSet('value', undefined) should be interpreted as an attempt to set the value, rather than get it.

Reported by: mattlunn Owned by:
Priority: low Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: attributes value Cc:
Blocked by: Blocking:

Description

For methods such as val, html and attr, if the value parameter is undefined the call is interpreted as a get action rather than a set, and the jQuery chain is broken.

As a value for value was provided (shame it was undefined), I would expect jQuery to treat the action as a set.

See http://www.jsfiddle.net/3CAta/ as an example.

Regards, Matt

Change History (2)

comment:1 Changed 9 years ago by addyosmani

Keywords: attributes value added
Priority: low
Resolution: wontfix
Status: newclosed

I believe the reason for this fallback is that:

  1. If a valid value previously existed, rather than undefined being set in it's place (what would undefined display as? empty string? null? undefined?) the previous value is instead still visible/accessible as a clean fallback.
  1. Developer's are able to check for undefined values prior to setting them as the value of any attribute using less than a line of code. By not setting some default value for undefined in this case, we leave it open to you to sanitize the data in the way you see best fit (for example, as I mentioned above, you could decide whether a value of undefined resulted in an empty string, saying 'invalid entry' and so on).

Closing as I believe the present behavior offers more advantages than disadvantages.

comment:2 Changed 9 years ago by dmethvin

This behavior has changed in 1.4.3 though, most setters no longer treat the setter .method(undefined) the same as a getter .method() although that obviously has its own tradeoffs since we have to decide what setting something to undefined does.

See #4388 and #4130; if we've missed some then open a specific ticket and reference those two.

Note: See TracTickets for help on using tickets.