View Full Version : Disable/enable form elements
javaboy
11-25-2002, 02:29 PM
Hi everyone.
I would like to know if I could Disable/enable a form text field if the user changes the selection from a drop down menu,
let's say I have a text field I want to be Disabled and only if the user selects a specific value from the drop down menu the field will get enabled if the user will select anything else the field will stay Disabled
Thanks and please reply
Javaboy
RadarBob
11-25-2002, 04:30 PM
<head>
<script type="text/javascript">
function toggleTextField (theOption) {
var theCommand = theOption.options[theOption.selectedIndex].value;
switch (theCommand) {
case "certain Value" :
document.formName.myTextField.disabled = true;
break;
default :
document.formName.myTextField.disabled = false;
}
}
</script>
</head>
<body>
<select name="textControl" onClick = "toggleTextField(this);">
<option value="certain Value">yabadabadoo</option>
// other options
</select>
. . .
<input type="text" name="myTextField" disabled="false"> //default editable
</body>
The "switch" structure makes the code flexible in case you want to do various different things depending on the pulldown selection.
javaboy
11-25-2002, 04:47 PM
Thanks RadarBob...
I copied and pasted the whole code but I get an error when I tried to make a selection.
ON THE OTHER HAND, how about if I select a specific value from the drop down menu the text field will be required but if I select any other value the text field will be optional...
is it possible?
RadarBob
11-25-2002, 05:43 PM
Originally posted by javaboy
I copied and pasted the whole code but I get an error when I tried to make a selection.
Serves you right; actually using code I wrote. You should assume that, unless explicitly stated, code on this forum was not tested. Besides there is definitely some customization needed to work inside your page, and I don't know what you did. But the basic technique / premise of the code is solid.
. . .
ON THE OTHER HAND, how about if I select a specific value from the drop down menu the text field will be required but if I select any other value the text field will be optional...
Well, you have to decide how you will be able to tell if a field is required or not. Just checking the selected value may work OK, but the user can select any value at any time, unless it's a multiple select but you didn't say. So I'd think some more about just what it is I want to happen, and when.
I'd say you probably want to do form validation during SUBMIT. Check the value of the select box and if it's the proper one, check if the text field is empty. If user is supposed to enter text, put up an alert box.
javaboy
11-25-2002, 06:55 PM
I'm sorry RadarBob..
The script is perfect; I just had to put in a name for the form
Thanks again
Javaboy
glenngv
11-26-2002, 03:42 AM
you can also use the form property of form fields to reference the form object the field is on, so that you don't have to specify the form name in the code:
function toggleTextField (theOption) {
var theCommand = theOption.options[theOption.selectedIndex].value;
switch (theCommand) {
case "certain Value" :
theOption.form.myTextField.disabled = true;
break;
default :
theOption.form.myTextField.disabled = false;
}
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.