View Full Version : Enable/Disable
ChetUbetcha
02-08-2008, 04:55 PM
I have a dynamic checkbox array (php) with name="comment[]" for each element. I created Check All / Uncheck All buttons that work fine. When the Check All is checked, the checkboxes are all checked and the disabled Modify button is enabled.
Now, instead of using Check/Uncheck All buttons, how do I create the JavaScript so that when at least ONE checkbox is manually checked, it enables the Modify button? And when I manually go through and uncheck the buttons, after the last selected checkbox is unchecked, it disables the Modify button once more?
Thanks!
hemebond
02-08-2008, 10:21 PM
So, really what you want is for the "modify" button to be disabled when all or none of the "comment[]" checkboxes are checked?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>132879</title>
</head>
<body>
<form>
<fieldset>
<legend>Checkboxes</legend>
<label>Checkbox 1 <input name="comment[]" type="checkbox"></label>
<label>Checkbox 2 <input name="comment[]" type="checkbox"></label>
<label>Checkbox 3 <input name="comment[]" type="checkbox"></label>
<input id="modify" type="button" value="modify">
</fieldset>
</form>
<script type="text/javascript">
var commentCheckboxes = document.getElementsByName('comment[]');
for (var i = 0; i < commentCheckboxes.length; i++) {
commentCheckboxes[i].addEventListener('change', updateModifyButton, false);
}
function updateModifyButton() {
var count = 0;
for (var i = 0; i < commentCheckboxes.length; i++) {
if (commentCheckboxes[i].checked) {
count++;
}
}
if (count == 0 || count == commentCheckboxes.length) {
document.getElementById('modify').disabled = true;
}
else {
document.getElementById('modify').disabled = false;
}
}
</script>
</body>
</html>
rnd me
02-09-2008, 07:55 PM
Or, simply put this attribute in each of your checkbox tags
onchange="if(this.checked){document.forms[0].modify.enabled=true;}"
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.