hi; i have a form with 2 fields: color: <select name="color"> <option value=1>red</option> <option value=2>black</option> <option value=3>blue</option> <option value=4>white</option> <option value=3>yellow</option> </select> transparency: <input type="text" name="transparency" value=""> Code (markup): i want to disable the transparency field if color is not 1, 3 or 4; could someone help me please?
I didn't test this, but it should work. If not it should point you in the right direction. Make sure to check the values of your options, I saw you had 2 options with value=3. Not sure if this was supposed to be like that, or if that 2nd value=3 was supposed to be value=5. Also, you will need to give your form the name attribute and change formname in the javascript to whatever you name your form. Hope this helps. <script language="javascript"> function chkColor(selObj) { switch(selObj.options[selObj.selectedIndex].value) { case 2: formname.transparency.disabled = true; break; case 5: formname.transparency.disabled = true; break; } } </script> color: <select name="color" onchange="chkColor(this);"> <option value=1>red</option> <option value=2>black</option> <option value=3>blue</option> <option value=4>white</option> <option value=5>yellow</option> </select> transparency: <input type="text" name="transparency" value=""> Code (markup):
I forgot that in the code above it will only set the transparency field to disabled, but never switch back to enabled if 1, 3, or 4 are selected. This should fix that. <script language="javascript"> function chkColor(selObj) { switch(selObj.options[selObj.selectedIndex].value) { case 1: formname.transparency.disabled = false; break; case 2: formname.transparency.disabled = true; break; case 3: formname.transparency.disabled = false; break; case 4: formname.transparency.disabled = false; break; case 5: formname.transparency.disabled = true; break; } } </script> Code (markup):