Deselect all items in a Select List
Say you want to have a "unselect all" button for a select list, how would the JavaScript work? I just spent too long trying to figure out how to do this in IE. As usual, I got it to work in Firefox right away, but IE doesn't work the same way.
Here's the code that worked in Firefox, but not in IE:
for (var i = 0; i < templateList.options.length; i++) {
templateList.options[i].selected = false
alert(templateList.options[i].selected)
}
I tried all kinds of variations of the above, and nothing worked in either IE 6 nor 7. I finally found this, which I like better anyway:
And that works in both Firefox and IE. I can't wait for all the browsers to pass the Acid 3 test...
Jake Munson
37 Yrs old
document.getElementById("templateList").selectedIndex = 1;
regards,
larry
You're using the second shorter snippet? This one worked for me in Firefox.
this work for me
i m facing some problems inside transfer options in my code,so i gone through with your code and i apply it to my code as well but my code is not going inside the for loop of your code.its not taking the value of templateList.options.lengtgh,can you help me how to reach inside the for loop of your code....
var templateList = document.getElementById('templateList');
for (var i = 0; i < templateList.options.length; i++)//inside this my code is not reachable
{
templateList.options[i].selected = false;
alert(templateList.options[i].selected)
}
That for loop didn't work (it was the code that I was trying that would NOT work. This is the code I found that works:
document.getElementById("templateList").selectedIndex = -1