Select All/None checkboxes in a list

Discussion in 'Programming' started by Dobey, Jun 11, 2012.

  1. #1
    I have a form that has a choice to check all of the items in a list or check none of the items in the list. I cannot get his to work and was wondering if someone can help me. Here is the snippet of code.



    <span class="optionsToggle">
    <input type="checkbox" onclick="toggleChecked(this.checked " name="do_toggle" value="#" id="do_toggle" class="noborder" />
    <label for="do_toggle">Select All
    </label>
    </span>
    <span class="optionsToggle">
    <input type="checkbox" onclick="javascript:toggleSelect(this, 'getDirectiveOrg:list', false);" name="do_toggle" value="#" id="do_toggle" class="noborder" />
    <label for="do_toggle">Select None
    </label>
    </span>
    <br />
    <div style="float: left; margin-right: 2em;">

    <input type="checkbox"
    name="getDirectiveOrg:list"
    value="AD" class="noborder"
    id="getDirectiveOrg_1_1" />
    <label for="getDirectiveOrg_1_1">AD</label>
    <br />


    <input type="checkbox"
    name="getDirectiveOrg:list"
    value="BU" class="noborder"
    id="getDirectiveOrg_1_2" />
    <label for="getDirectiveOrg_1_2">BU</label>
    <br />
     
    Dobey, Jun 11, 2012 IP
  2. jezwebb

    jezwebb Member

    Messages:
    21
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #2
    Try this:

    <script>
    function toggleCheckboxes() {
    // written by Daniel P 3/21/07
    // toggle all checkboxes found on the page
          var inputlist = document.getElementsByTagName("input");
          for (i = 0; i < inputlist.length; i++) {
           if ( inputlist[i].getAttribute("type") == 'checkbox' ) {    // look only at input elements that are checkboxes
                if (inputlist[i].checked)    inputlist[i].checked = false
                else                                inputlist[i].checked = true;
            }
        }
    }
    </script>
    <span class="optionsToggle">
    <input type="checkbox" onclick="toggleCheckboxes();" class="noborder" />
    <label for="do_toggle">Toggle
    </label>
    </span>
    
    
    <br />
    <div style="float: left; margin-right: 2em;">
    
    
    <input type="checkbox"
    name="getDirectiveOrg:list"
    value="AD" class="noborder"
    id="getDirectiveOrg_1_1" />
    <label for="getDirectiveOrg_1_1">AD</label>
    <br />
    
    
    
    
    <input type="checkbox"
    name="getDirectiveOrg:list"
    value="BU" class="noborder"
    id="getDirectiveOrg_1_2" />
    <label for="getDirectiveOrg_1_2">BU</label>
    Code (markup):
     
    jezwebb, Jun 12, 2012 IP
    Rukbat likes this.