Deselecting options from a select element in jQuery

Oh boy, yet another IE7 gotcha.

I wrote this piece of jQuery to code to deselect selected option(s) from a select element:

$("[ name = 'selectFieldName' ] option:selected ").attr( 'selected', '' );

Which works fine in Firefox, but then when the QA tested it, it doesn’t work for him and of course he tested in IE7 (which is a good thing).

So googled around a bit, find out this article on jQuery Google Group:

http://groups.google.com/group/jquery-en/browse_thread/thread/af73f2b57473ffb6

So I tweak the code to:

$("[ name = 'selectFieldName' ]").attr('selectedIndex', '-1');

And it works for both FF and IE7.

This entry was posted on Wednesday, November 12th, 2008 at 1:39 pm and is filed under jQuery. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.