Passionate Development From Journeyman to Master

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:

[code=”js”]$("[ name = ‘selectFieldName’ ] option:selected ").attr( ‘selected’, ‘’ );[/code]

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:

[code=”js”]$("[ name = ‘selectFieldName’ ]").attr(‘selectedIndex’, ‘-1’);[/code]

And it works for both FF and IE7.

jquery