...

View Full Version : Checkbox elements and disabled submit button



yellow5
07-08-2005, 08:46 AM
I have a list of generated checkboxes, and a "Delete" button at the bottom that starts off disabled. I would like for the Delete button to be enabled whenever a checkbox is selected, and then go back to being disabled should there be no checkboxes selected at the moment. Here's the code I have:



function enableButton(form) {
for (i=0; i<document.form.length; i++) {
if (document.form.elements[i].checked == true) {
document.form.deleteButton.disabled=false;
} else {
document.form.deleteButton.disabled=true;
}
}


and
<input type='checkbox' onClick='enableButton('form1')'> for each generated checkbox. The form is named "form1."

Any help on this would be greatly appreciated.

Mr J
07-08-2005, 09:33 AM
Please try the following



function enableButton(formname) {
myForm=document.forms[formname]
chk=0
myForm.deleteButton.disabled=false;

for (i=0; i<myForm.length; i++) {
if (myForm.elements[i].checked == true) {
chk=1
}
}

if(chk==0){
myForm.deleteButton.disabled=true;
}

}

Harry Armadillo
07-08-2005, 09:40 AM
Or you could pass the form by reference (instead of by name).
<script type='text/javascript'>
function enableButton(form) {
var a=true;
for (var i = 0; a && i < form.length; i++)
if (form.elements[i].type == 'checkbox')
a=!form.elements[i].checked;
form.deleteButton.disabled=a;
}
</script>
<form>
<input type='checkbox' onClick='enableButton(this.form)'>
<input type='button' name='deleteButton' disabled='disabled'>
</form>

yellow5
07-08-2005, 10:03 AM
Thanks for the help guys. Passing the form by reference worked like a charm. I really appreciate it!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum