Reputation: 57
I have here a code that whenever I checked a checkbox the button will enable but if I unchecked it the button will be disabled. My problem is that, when I add another checkbox the disable and enable of button doesn't work. The output should be whenever I checked one or more checkbox the button will enable but if there's no checkbox that has been checked the button should be disabled. Anyone knows how I can do that?
Here's my code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function Disab(id)
{
if(document.form1.cbox.checked)
{
document.form1.Button1.disabled=false
}
else {
document.form1.Button1.disabled=true
}
}
</SCRIPT>
</HEAD>
<BODY TEXT="000000" BGCOLOR="FFFFFF">
<form name="form1">
<div align="center">
<input name="cbox" type="checkbox" id="cbox2" value="checkbox" onClick=Disab(this.id);>
Click here to Enable/Disable Button<br /><br />
<input type=button onClick="alert('Button pressed!')" value="Button" name="Button1" disabled>
</div>
</form>
</BODY>
</HTML>
Upvotes: 0
Views: 635
Reputation: 8116
My problem is that, when I add another checkbox the disable and enable of button doesn't work.
You're trying to enable/disable the button if one or more checkboxes are checked?
function verifyChecked() {
var inputs = document.querySelectorAll("#myForm input[type='checkbox']");
var atLeastOneChecked = false;
for(var i = 0; i < inputs.length && !atLeastOneChecked; i++) {
if(inputs[i].checked) {
atLeastOneChecked = true;
}
}
document.form1.Button1.disabled = !atLeastOneChecked;
}
Upvotes: 0