Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#8293 closed bug (wontfix)

val(null) on select-elements behaves differently in jquery 1.5

Reported by: seidel@… Owned by: seidel@…
Priority: low Milestone: 1.next
Component: attributes Version: 1.5
Keywords: Cc:
Blocked by: Blocking:

Description

In jquery 1.4.2 it was possible to blank out a select-element by calling val(null).

In jquery 1.5 val(null) always selects the first option-element.

I'm not sure if this is a bug but definitely a change of behavior.

Change History (4)

comment:1 Changed 8 years ago by addyosmani

Owner: set to seidel@…
Priority: undecidedlow
Status: newpending

Thanks for submitting a ticket to the jQuery Bug tracker! Could you please supply us with a reduced test case on jsFiddle.net that reproduces that issue you're experiencing?. We'll be happy to investigate further as soon as we have this.

comment:2 Changed 8 years ago by jitter

Component: unfiledattributes
Resolution: wontfix
Status: pendingclosed

test case

This behavior already changed with 1.4.3.

The documentation for .val() reads:

value A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked.

So passing null to val() was never a valid thing to do, the behavior when passing null also wasn't specified and thus can not be relied upon. And apparently this didn't work in IE. That's why with the fix for #5163 this unspecified behavior changed.

So if you want to blank out a select which doesn't have an empty field (as the select #two in my test case) you are better advised to do $(selectorForSelect).attr("selectedIndex", "-1")

comment:3 Changed 8 years ago by anonymous

Thanks you for the quick followup. That passing null to val() is not supported is actually what I expected since it wasn't mentioned in the docs.

Keep up the great work!

comment:4 Changed 8 years ago by dmethvin

As of jQuery 1.6, the correct solution is $(selectorForSelect).prop("selectedIndex", "-1")

Note: See TracTickets for help on using tickets.