Skip to main content

Bug Tracker

Side navigation

#8293 closed bug (wontfix)

Opened February 16, 2011 11:12AM UTC

Closed February 17, 2011 01:42PM UTC

Last modified May 27, 2011 02:19AM UTC

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

Reported by: seidel@prolan.de Owned by: seidel@prolan.de
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.

Attachments (0)
Change History (4)

Changed February 16, 2011 06:26PM UTC by addyosmani comment:1

owner: → seidel@prolan.de
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.

Changed February 17, 2011 01:42PM UTC by jitter comment:2

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")

Changed February 18, 2011 07:58AM UTC by anonymous comment:3

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!

Changed May 27, 2011 02:19AM UTC by dmethvin comment:4

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