...

View Full Version : unselect single option in select box



frontline
03-02-2003, 09:36 AM
hello
i have this simple script i only need to unselect the selected option box
with no luck ..
what is wrong here ?
<HTML>
<HEAD>
<script>
function foo(){
var oSelect=document.oForm.blah;
for(var i=0;i<oSelect.length;i++){
oSelect.options[i].selected=false;
}
}
</script>
<TITLE> New Document </TITLE>
</HEAD>
<form name="oForm">
<SELECT NAME="blah" size="5" style="width:20px">
<option >1</option>
<option selected>2</option>
<option>3</option>
</SELECT>
</form>
<input type=button value="Unselect" onclick="foo();">
</BODY>
</HTML>

thanks!

chrismiceli
03-02-2003, 07:20 PM
i don't think you can unselect an option in a select box once something has been selected, make an option like this
<option>-----------</option>
and move the focus to that like this
oSelect.options[0].focus();

glenngv
03-03-2003, 01:15 AM
if the select is only single select (not multiple), just set the selectedIndex to -1 without doing a loop.


<input type=button value="Unselect" onclick="this.form.blah.selectedIndex=-1">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum