How to disable or enable multipe form elements?

The example shows how to change the status of several form elements from another form element.

<html> 
<head> 
<script language="javascript"> 
function DisableText()
{
var count = document.forms[0].elements.length;
for (i=0; i<count; i++) 
  {
var element = document.forms[0].elements[i]; 
if (document.form1.check1.checked == true) 
    { 
	if (element.type == "text") 
	{ element.disabled=true; } 
    } 
else 
  	{ element.disabled=false; }
  }
}
</script> 
</head> 
<body> 
<form name="form1"> 
<input type="checkbox" name="check1" onClick="DisableText()"/>  
<input type="text" name="text1" id="text1" value="Type some text"> 
<input type="text" name="text1" id="text2" value="Type some text">
<input type="text" name="text1" id="text3" value="Type some text">
</form> 
</body> 
</html>


The function checks if the checkbox control is checked or not. If it is, it iterates through all the controls of the form and disables the ones of type text ie. all textboxes.

Related threads:
disabling form elements

JavaScript FAQ

 
Printer friendly version of the FAQ-JavaScript-Disable-Enable-Multiple-Form-Elements page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.
Computer Professionals: Are you owed Overtime?
Federal and State Laws may allow computer professionals to collect overtime. Our law firm is experienced, and has initiated class action lawsuits against some of the largest computer companies to collect back pay and overtime. Strictly Confidential.
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.