[thelist] Script to select all options in select dropdown

Miller, Bart MillerB at bek.com
Thu, 2 Dec 1999 09:34:13 -0600


I am trying to write a script to select all options in a dropdown menu.
This would be the equivalent of holding down the control key as you click on
each item.  But for some reason the code below only selects the last one in
my loop and not all of them as I would expect. Any ideas?  Here's the code:

<script>
function selectall() {
tot = document.testform.subject.options.length ;
for (i=0;i<tot;i++) {
document.testform.subject.options[i].selected = true;
}
}
</script>

<form name=testform>
<SELECT NAME = "subject" size=4>
<OPTION>First
<OPTION>Second
<OPTION>Third
<OPTION>Fourth
</SELECT>
</form>
<a href="#" onclick="selectall()">Select All</a>