SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tricky select all/select none subset of checkboxes (all have same name)

    I've got dozens of checkboxes on a page, all with the same name (ids are different though). The name is the same in order to facilitate some PHP and Expression Engine coding on the form action page.

    Is there some way I can trigger a select all/select none on a subset of them? I can position them within named divs, like this:

    HTML Code:
    <div id="checkboxes_14">
      <input type="checkbox" name="checkbox[]" value="1" id="checkbox_1" />
      <input type="checkbox" name="checkbox[]" value="2" id="checkbox_2" />
    </div>
    
    <div id="checkboxes_15">
      <input type="checkbox" name="checkbox[]" value="3" id="checkbox_3" />
      <input type="checkbox" name="checkbox[]" value="4" id="checkbox_4" />
    </div>
    I have some code for select all/select none, but it will do so for all checkboxes, not a subset of them.

    Thanks.
    :::::::::::::::::::::::::::::::::::::::::::::::::::
    Versa Studio
    ExpressionEngine experts
    bas (at) versa studio dot com

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    var div = document.getElementById('checkboxes_14');
    var inputs = div.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type === 'checkbox') {
    		inputs[i].checked = true;
    	}
    }
    Last edited by paul_wilkins; Apr 29, 2008 at 18:41.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 4midori View Post
    I've got dozens of checkboxes on a page, all with the same name (ids are different though). The name is the same in order to facilitate some PHP and Expression Engine coding on the form action page.

    Is there some way I can trigger a select all/select none on a subset of them?
    CheckBoss was written to do that without needing to segregate any elements.
    Your square bracket [] naming makes the syntax a bit denser than normal, but say to toggle the third and fifth checkboxes from a master checkbox in the same form, use:

    Code:
    <input type='checkbox' onclick="checkBoss(this, [ this.form['checkbox[]'][2], this.form['checkbox[]'][4]  ], 'toggle')">
    Tab-indentation is a crime against humanity.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Code:
    var div = document.getElementById('checkboxes_14');
    var inputs = div.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type = 'checkbox') {
    		inputs[i].checked = true;
    	}
    }
    Assignment operator used.
    Tab-indentation is a crime against humanity.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Thank you Logic Ali, I've updated the original code for this.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all. This helps a lot.
    :::::::::::::::::::::::::::::::::::::::::::::::::::
    Versa Studio
    ExpressionEngine experts
    bas (at) versa studio dot com


Bookmarks

Posting Permissions

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