www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Select All checkboxes from array

  1. #1
    Join Date
    Aug 2007
    Posts
    94

    resolved [RESOLVED] Select All checkboxes from array

    Hi all

    I need to be able to get all my checkboxes which are in an array and which have the name:

    name="personselected[]"

    to be all checked when a button or link is clicked.

    I have looked around and can only find examples of where the checkboxes are not in arrays. I am quite a newbie at JS so any advice would be greatly apprecated. The closet I have found to what I need is below but it won't work due to my checkboxes being an array.

    Code:
    function checkAll(field) {
    	for (i = 0; i < field.length; i++)
    	field[i].checked = true;
    }
    
    <input name="CheckAll" type="button" value="Select All" onclick="checkAll(document.myform.personselected);" />
    Any advice will be greatly appreciated.

    Thanks

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Lightbulb Here's an idea ...

    See if you can modify this example to your needs:
    PHP Code:
    <html>
    <
    head>
    <
    title>CBox.js Test</title>
    <!-- 
    script type="text/javascript" src="../CBox.js"></script -->

    // CBox.js
    function getCBox(ids) {
      var 
    sel document.getElementById(ids);
      var 
    str '';
      if (
    sel.checked == true) { str sel.value; }   // alert(sel+'\nvalue:'+str);
      
    return str;
    }

    <
    script type="text/javascript">
    function 
    CheckAllCheckboxes() {
      var 
    str '';
      
    str += getCBox('CB1')+',';
      
    str += getCBox('CB2')+',';
      
    str += getCBox('CB3')+',';
      
    str += getCBox('CB4')+',';
      
    str += getCBox('CB5')+',';
      return 
    str;
    }
    </script>
    </head>
    <body>
    <h1>CBox.js Test</h1>
    <p />
    <input type="checkbox" id="CB1" value='1'>One
    <input type="checkbox" id="CB2" value='2'>Two
    <input type="checkbox" id="CB3" value='3'>Three
    <input type="checkbox" id="CB4" value='4'>Four

    <input type="checkbox" id="CB5" value='5'>Five
    <button onClick="document.getElementById('Result1').value=CheckAllCheckboxes()">Check</button>
    <input id="Result1" type="text" value="">Results
    </body>
    </html> 
    If not, then post your example that doesn't work!

  3. #3
    Join Date
    Aug 2007
    Posts
    94
    Thanks for the response JMRKER but I can't set the number of checkboxes as this is dynamically built. There could be 1 to a hundred or more so I can't see how the CheckAllCheckboxes function will work.

    Any other solutions out there?

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Lightbulb Modify this then ...

    When you dynamically create the checkboxes, use a counter to label them.
    As in CB0, CB1, ..., CB98, CB99, etc.
    Need an example of how you are creating the dynamic CBoxes to give specific example.
    But then you can:
    PHP Code:
    // nCbox is the total number to set | reset the dynamically created checkboxes 
    // flag passed as true | false to set | reset all checkboxes
    function CheckAllCheckboxes(nCboxflag) {
      for (
    i=0i<nCboxi++) {
        
    document.getElementById('CB'+i).checked flag;
      }

    // test for specific CBxxx checked | not checked with this following function
    function CheckboxStatus(IDS) {
      return 
    document.getElementById(IDS).checked;


  5. #5
    Join Date
    Aug 2007
    Posts
    94
    Thats were it gets tricky as I have them as follows:

    Code:
    <input id="per_124872"  name="personselected[]" type="checkbox" class="percheckbox" value="124872" onClick="if(!this.checked)document.getElementById('rem_124872').checked=false;" />
    So, I could have upto a hundren or more of these, and where you see the per_NUMBER, this is the ID from the database of the record.

    Any suggestions?

    I appreciate your help and advise.

  6. #6
    Join Date
    Mar 2005
    Posts
    767
    Code:
    function checkAll(field) {
    	for (i = 0; i < field.length; i++)
    	field[i].checked = true;
    }
    
    <input name="CheckAll" type="button" value="Select All" 
    onclick="checkAll(document.myform['personselected[]'])" />
    Stop thinking, start drinking.

  7. #7
    Join Date
    Aug 2007
    Posts
    94
    Orc Scorcher you are a legend.

    Thank you.

  8. #8
    Join Date
    Nov 2009
    Posts
    1
    Thankyou Orc Scorcher that was exactly I was looking for. just a couple of [ and ] at the right place...

    Thanks once more... Its really funny...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles