views:

95

answers:

2

I have a web page with several HTML Select controls that all allow multiple selection. Next to each control we have a "Clear" button that de-selects all selected items in the control, with the onclick that looks something like this:

<input type=button value="Clear" size=5 onclick="selectOptions('n.MyControl', false)">

The called Javascript code looks like this:

function selectOptions(controlName, bSelectItems)
{
    for (i=0; i < document.myForm[controlName].options.length; i++)
    {
        document.myForm[controlName].options[i].selected = bSelectItems;
    }

}

It works really well with the Select controls that allow multiple selection, but I just added a Select control that does not allow multiple selection (i.e. does not have the "multiple" attribute), and this code does not clear it. If I add the "multiple" attribute, the Clear button starts working again, so I know it has something to do with the fact that I am not allowing multiple items to be selected.

My question is, why does the above code not work and more importantly, how can I get my new Select control cleared in JavaScript?

+2  A: 

The reason your code doesn't work is because you are trying to iterate over each option and do something, but since (like you said) the multiple attribute isn't set, it doesn't let you modify multiple options.

As for fixing it, you can try to get the selected option and then just deselect that single option.

Update: I don't know for sure that this is the ideal solution, but it seems to work for me in IE8.

document.getElementById('myselect').selectedIndex = -1;

Of course you can get the select element whichever way works best for you, but just set the selectedIndex attribute to -1.

Jared
Thanks for the response; it makes sense but I'm having difficulty implementing it. I've tried mySelect.selectedIndex = -1, mySelect.selected.selected = false, and a few others. Any specific code suggestions?
Patrick
When you have the select box that does not allow for multiple selection, does it become a standard dropdown select or still show as if it a multiple select (with several options shown at a time) but only allow for one to be selected?
Jared
The latter (it shows as a box as though you can multi select even though you can only select one), probably because I have the size attribute set to 5 (I'll modify my question to include it).
Patrick
The implementation would be `mySelect.options[mySelect.selectedIndex].selected = false;`
Stan Rogers
Check the update in my answer.
Jared
Hey, that worked! I had to use document.MyForm(MySelect)... rather than GetElementById, but setting selectedIndex to -1 did the trick. Thanks a million!
Patrick
Not a problem. Glad that I could help!
Jared
+1  A: 

There is no such thing as a cleared single-<select> box. One option will always be selected as long as there are any options in the select at all. If no options have selected set at first load time, the first option will automatically get selected.

The usual approach is to have the first option as a dummy-option like <option value="">(Please select a thing)</option>, and reset to that (with select.selectedIndex= 0).

bobince
Are we talking about the same thing? My single-select Select control never has an option selected when the page first loads. I'm using IE8, BTW.
Patrick
I can assure you that given the markup `<select><option>a</option><option>b</option></select>`, when the page first loads, option `a` *will* be selected. If it *looks* like there is no selection, that is probably because there is a blank option at the top.
bobince
The reason he is not seeing an option selected is because he has `size=5`, therefore an option is not selected. If it were a standard dropdown box, then yes it would have an option selected.
Jared