faqts : Computers : Programming : Languages : JavaScript : Forms : Checkboxes

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

20 of 33 people (61%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

If checkbox is ticked, how do I disable multiple text boxes?

Apr 15th, 2003 08:58
Gauthier Delamarre, Steve Woolston,


To disable any form element by checking a checkbox input, the best way  
is certainly to use a user defined function like this :  
  
<script>  
  
function disableEntries(formObj)  
{  
  // Test if the checkbox were checked  
  if(formObj.myCheckbox.checked)  
  {  
    // The box was checked, let's disable some text elements  
    formObj.firstTextEntry.disabled = true  
    formObj.secondTextEntry.disabled = true  
  }  
  else  
  {  
    // The box was unChecked  
    formObj.firstTextEntry.disabled = false  
    formObj.firstTextEntry.disabled = false  
  }	  
}  
  
<!-- Now, we have to create the form itself -->  
<form name=myForm action=whatIwant.php>  
First field : <input type=text name=firstTextEntry><br>  
First field : <input type=text name=secondTextEntry><br><br>  
<input type=checkbox name=myCheckBox 
onClick="disableEntries(this.form)"> Disable /  
Enable these two fields  
</form>  
  
  
This code should do the trick (untested, but ... used many times in  
other context).  
  
Hope this help