Clear SELECT (multiple)

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Clear SELECT (multiple)

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply