Click to See Complete Forum and Search --> : disable one option of <select> list


ferouzmj
March 3rd, 2005, 08:46 PM
here is what i have: i have two <SELECT> lists - select1 and select2. what the JavaScript function does is disable select2 if "Summary" is chosen from select1. And here is what i need - when "Summary" is chosen from select1 i don't want to disable select2 but disable one of the options(let's say "two") of select2. sounds simple enough but i have not been able to get solid help on other forums. thanks.

<html>
<body>
<script language="JavaScript">
<!--
function disable()
{
var indice = document.frm.select1.selectedIndex;
//var optionDESC = document.frm.select2.options[1];
if(document.frm.select1.options[indice].value=="Summary")
{
document.frm.select2.disabled=true;
}
else
{
document.frm.select2.disabled=false;
}
}
-->
</script>

<form name="frm">

<select name="select1" onchange="disable()">
<option value="Detail">Detail</option>
<option value="Summary">Summary</option>
</select>

<br><br>

<select name="select2">
<option value="1">one</option>
<option value="2">two</option>
</select>

</form>

</body>
</html>

khp
March 4th, 2005, 07:47 AM
You should be able to use something like removeChild something like

document.frm.select2.removeChild(document.frm.select2.options[1]);


The alternative would be to keep 2 different versions of select2 in your document, always keep one disabled and switch between the two, when "Summary" is selected/deselected in select1.

ferouzmj
March 4th, 2005, 12:01 PM
Thanks KHP, it does the trick except it removes the child completely and does not return it when other than "summary" is reselected from select1. But i'll have to tweak it a little bit. Thanks again.

khp
March 4th, 2005, 01:25 PM
Yes, you do of course have to add it to the select, when summary is deselected, you can use the insertBefore method for this http://www.mozilla.org/docs/dom/domref/dom_el_ref47.html#1028897 , to do that you will of course have to maintain a reference to the deletede option, in the javascript when you delete it.