Select/deselect a check box group using JavaScript

This article example shows how to implement select/deselect a check box group using one main check box.
Example given here has tested only on IE and Firefox so you may want to try and test it before using this in your web pages . Please post your comments on this article if you have any.



Code:
<script type="text/javascript">
<!--
 function CheckORUnCheck (chkBox) {
     var arrVals = document.getElementsByTagName("input");
     for (x=0; x<arrVals.length; x++) {
         if (arrVals[x].type == 'checkbox') {
           if (chkBox.checked==true){
             // select mode
             arrVals[x].checked = true;
          }else{
          //deselect mode
             arrVals[x].checked = false;
          }         
         }
      }         
 }
 -->
 </script>

 <strong>Play List:</strong>
 
 <form id="checkboxtest" method="get" action="">
   <input type="checkbox" name="chk_main" id="chk_main" value="" onClick="CheckORUnCheck(this);" />
   Select or Deselect all songs<br />
   <input type="checkbox" name="chk_1" id="chk_1" value="1" /> Song 1 <br />
   <input type="checkbox" name="chk_2" id="chk_2" value="2" /> Song 2 <br />
   <input type="checkbox" name="chk_3" id="chk_3" value="3" /> Song 3 <br />
   <input type="checkbox" name="chk_4" id="chk_4" value="4" /> Song 4 <br />
   <input type="checkbox" name="chk_5" id="chk_5" value="5" /> Song 5 <br />
 </form>
 

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net