Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple checkbox problem

Status
Not open for further replies.

Berras

Programmer
Feb 24, 2002
42
0
0
GB
Apologies in advance my javascript is not great.

I want to create a script to cycle through a number of checkboxes to disable them. The best I can do is:

for(i=0;i<20;i++) {
vax x = document.forms.myForm.box_;
x.disabled=true;
}

Where the checkboxes are named "box_0, box_1, box_2..." etc.
 
Sorry, that supposed to read:
var x =...
 
You're close... but have a few minor errors. Try this instead

Code:
for (var loop=0; loop<20; loop++) {
	document.forms['myForm'].elements['box_' + loop].disabled = true;
}

I removed the assignation, as it was not really needed.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If you don't want to limit it to a specific form, you can hit the whole page by doing the following.
Code:
function disableCheckboxes()
{
  // Using the DOM method, set a variable (array) to be all
  // input tags on the page.
  var inputs=document.getElementsByTagName("input");
  var i;
  for (i=0; i<inputs.length; i++)
  {
    // Only disable the inputs if they are checkboxes.
    if (inputs[i].type=="checkbox")
    {  inputs[i].disabled=true; }
  }// end for i
}// end disableCheckboxes

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top