...

View Full Version : unselecting all on multicombo



esthera
05-02-2007, 04:50 PM
I have multiple select box
where the first option is
<option selected value="">All</option>

is there anyway that if anything is selected other then all then it automatically deselects all?

glenngv
05-02-2007, 04:52 PM
function deselect(sel){
for (var i=0; i<sel.options.length; i++){
sel.options[i].selected = false;
}
}

esthera
05-02-2007, 05:20 PM
so what does this do and how do I use it?

glenngv
05-02-2007, 06:28 PM
It deselects all the options of the given select object.

<form>
<select name="select1">
<option selected value="">All</option>
<option selected value="1">item 1</option>
<option selected value="2">item 2</option>
<option selected value="3">item 3</option>
...
</select>
<input type="button" value="Deselect" onclick="deselect(this.form.select1);" />
</form>

esthera
05-02-2007, 06:53 PM
but is there anyway for me to automatically if any options are selected then the first option will be unselected?

glenngv
05-02-2007, 07:08 PM
I thought you want to deselect all options? Now you want to deselect the first option if anything are selected other than the first option?

function deselect(sel){
var selected = false;
for (var i=1; i<sel.options.length; i++){
if (sel.options[i].selected) {
selected = true;
break;
}
}
if (selected) {
sel.options[0].selected = false;
}
}

<form>
<select name="select1" size="3" multiple="multiple" onchange="deselect(this);">
<option value="">All</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
...
</select>
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum