Page 1 of 1

Clear SELECT (multiple)

Posted: Sat May 14, 2005 10:36 am
by MarK (CZ)
Hi all!

I'm having a problem with multiple SELECT. I need the javascript code to clear the field if there's already something selected.

Code: Select all

<select name=&quote;nV&#1111;perm_lang]&#1111;]&quote; id=&quote;perm_lang&quote; class=&quote;normal&quote; size=&quote;4&quote; multiple=&quote;multiple&quote;>
 <option value='1' selected='selected'>&nbsp;Czech</option>
 <option value='2' selected='selected'>&nbsp;English</option>
 <option value='3' >&nbsp;French</option>
 <option value='4' >&nbsp;German</option>
 <option value='7' >&nbsp;Swedish</option>
</select>
As you can see, there are already some options selected. What can I do if I want to send value as if there weren't anything selected?
I've already tried these but no luck:

Code: Select all

perm_lang.value = undefined;
perm_lang.value = &quote;&quote;;
perm_lang.value = new Array();
Thanks in advance :)

Posted: Sat May 14, 2005 11:49 am
by Chris Corbyn
I guess you can just do this but I haven't tested it...

Code: Select all

for (var x in el.options) {
    el.options&#1111;x].selected = false;
}

Posted: Mon May 16, 2005 9:58 am
by pickle
Regardless of whether something is selected or not, that variable will still be sent via POST. Usually, I put a blank entry in my select boxes, and give that option an 'impossible' value, such as -1 or 0. Then, when I'm processing the form, I handle the select boxes and treat a value of -1 or 0 (whichever the case may be), as if nothing was selected.