Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox elements and disabled submit button

    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:

    Code:
    	
    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
    Code:
    <input type='checkbox' onClick='enableButton('form1')'>
    for each generated checkbox. The form is named "form1."

    Any help on this would be greatly appreciated.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Please try the following


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

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

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


    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or you could pass the form by reference (instead of by name).
    Code:
    <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>

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help guys. Passing the form by reference worked like a charm. I really appreciate it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •