Select all the checkboxes using another checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niravmehtak
    New Member
    • Aug 2007
    • 2

    Select all the checkboxes using another checkbox

    Hi guys tell me how to select all checkbox in single checkbox button
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Moving threads from Articles to Forums

    You have posted your question in the Articles section rather than the Forum section.I have moved it across for you.

    Thanks please do not do the same again.

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by niravmehtak
      Hi guys tell me how to select all checkbox in single checkbox button
      Hi niravmehtak,

      This can be done I'm sure. What you need to do is set the value of all the other checkboxes on the clik of a controlling button.

      I would do this using document.getEle mentById('check box').innerHTML .

      So, on the controlling button you set the onclick() event to call a javascript function., into this function you can pass the ID's of the checkboxes you wish to set, then in a loop you can set the value to what it is not. So:

      [html]
      <form name="checkboxt est" action="target. html" methos="post>
      <input type="checkbox" id="checkbox1" value="test1" />
      <input type="checkbox" id="checkbox2" value="test2" />
      <input type="checkbox" id="checkbox3" value="test3" />
      <input type="checkbox" id="checkbox4" value="test4" />
      <input type="button" id="checkboxAll " value="select all" onclick="select All('checkbox1, checkbox2, checkbox3, checkbox4', 'checkboxAll' />
      </form>
      [/html]

      Then the javascript would look something like
      Code:
      function selectAll(pcChekcBoxList, pcControllingItem)
      {
      var laItems = pcCehckBoxList.split(',') 
      // creates an array
      lnArrayCntr = 0
      lnArrayLength = laItems.length - 1
      for(lnArrayCntr=0; lnArraycntr <= lnArrayLength; lnArrayCntr++)
      {
      if(document.getElementById(laItems[lnArrayCntr]).checked == "checked")
      {
      document.getElementById(laItems[lnArrayCntr]).checked = ""
      } 
      else
      {
      document.getElementById(laItems[lnArrayCntr]).checked = "checked"
      }
      if(document.getElementById(pcControllingItem).value == "Select All")
      {
      document.getElementById(pcControllingItem).value = "Unselect All"
      }
      else
      {
      document.getElementById(pcControllingItem).value = "Select All"
      }
      }
      }
      I haven't tested this, but the theory is correct and it should work. You may need to add some extra twiddles to set the button correctly. You could add an onclick to each checkbox that counts how many checkeboxes are checked. If it's all of them then set the button to "Unselect All", if it's not all of them then ensure that the button says "Select All". For this I would also use an array and a for loop.

      That's my suggestion, I hope it helps. If you go to the nth degree, as I mention here this should work well in all circumstances.

      Cheers
      nathj

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Shouldn't this be in the Javascript forum (with a changed title)?

        nathj, you could make it more generic. For example, see this example. Admittedly, that's probably more than what the OP needs, but no need to reinvent the wheel!

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Thread Title changed [Check box in php] to better describe the problem.
          Moved from Php-Forum.Thanks.

          @acoder:
          Thanks for opening my eyes back to the thread.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by ajaxrand
            Thread Title changed [Check box in php] to better describe the problem.
            Moved from Php-Forum.Thanks.

            @acoder:
            Thanks for opening my eyes back to the thread.
            No problem. I only found it because it was linked to from another thread.

            Comment

            Working...