Ticket #4130 (closed bug: fixed)
.val() should not return null as a value
| Reported by: | ptran123 | Owned by: | dmethvin |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.4.3 |
| Component: | attributes | Version: | 1.4.2 |
| Keywords: | val() | Cc: | |
| Blocking: | Blocked by: |
Description
I have an object with properties obj.FirstName and obj.LastName. FirstName is "mickey" and LastName is null. When I use $("#lastNameTextBox").val(obj.LastName), it displays null in the textbox. It should not display anything if the value is null. This logic worked OK with version 1.2.6. However, version 1.3.1 is showing null in this textbox.
Attachments
Change History
comment:1 Changed 4 years ago by ptran123
FYI, I'm using JSON.parse() from www.json.org to create the object.
comment:2 Changed 4 years ago by dmethvin
The 1.2.6 behavior was actually a bug. The $("#x").val(null) was treated as a getter rather than a setter, meaning it wasn't chainable when it should have been. If the value passed to .val() has the potential to be null or undefined, the best way to handle it is to use something like $("#lastNameTextBox").val(obj.LastName || "").
comment:3 Changed 4 years ago by dmethvin
- Priority changed from major to minor
- Component changed from unfilled to core
comment:5 Changed 3 years ago by dmethvin
- need changed from Review to Commit
- Owner set to dmethvin
Clarification for this bug: the remaining issue with 1.4.3 is that .val(undefined) is not chainable. This patch makes it chainable and defines it as .val("").
Patch: http://github.com/dmethvin/jquery/commit/43b06cf1c8694c953831b3509a1280b762763134
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

