...

View Full Version : array of checkboxes to enable/disable



cameronc
02-02-2005, 08:44 PM
Hi,

I'm trying to make what must be a simple checkbox enable/disable script. I have six checkboxes in one table cell and two in another table cell that are disabled when the page loads. Whenever one of the top six is checked, the two disabled come to life. This much I seem to have. The problem: I want the function to work so that if none of the top six are checked the two disabled boxes go back to being disabled after becoming enabled from previous onclicks. The function variable x is the checkbox's id that I'm sending to the function when one of the six is clicked. Here's what I've got, thanks in advance for whoever can help me out:

function toggler(x)

{

for (var i = 0; i < document.forms[0].length; i++)

{

if (document.frm.selects[x].checked)

{

document.frm.lineChoice.lc1.disabled = false;

document.frm.lineChoice.lc2.disabled = false;

}

else

{

document.frm.lineChoice.lc1.disabled = true;

document.frm.lineChoice.lc2.disabled = true;

}

}

}

hemebond
02-02-2005, 09:14 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>51508</title>
<!--[if IE]><script type="text/javascript">var ie = true;</script><![endif]-->
</head>
<body>
<form>
<fieldset id="controls">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</fieldset>

<fieldset id="toggles">
<input type="checkbox">
<input type="checkbox">
</fieldset>
</form>

<script type="text/javascript">
var controls = document.getElementById("controls").getElementsByTagName("input");
var toggles = document.getElementById("toggles").getElementsByTagName("input");

for(var i = 0; i < controls.length; i++)
{
if(!window.ie)
{
controls[i].addEventListener("click", toggle, true);
}
else
{
controls[i].onclick = toggle;
}
}

function toggle()
{
var checked = false;
var i = 0;

for(i = 0; i < controls.length; i++)
{
if(controls[i].checked == true)
{
checked = true;
break;
}
}

for(i = 0; i < toggles.length; i++)
{
toggles[i].disabled = !checked;
}
}

toggle();
</script>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum