Please Log In to save to favorites
TAGs
checkbox, check, uncheck, all


Versions
Check and uncheck all checkboxes at once
by steven
If you work with a page on your website or application that has a lot of checkboxes you might want to have the option to check or uncheck all the checkboxes at once. The following piece of code takes care of that problem.
      
    1.   function CheckAll()
    2.   {
    3.   count = document.trips.elements.length;
    4.    for (i=0; i < count; i++)
    5.    {
        if(document.trips.elements[i].checked == 1)
    7.    {document.trips.elements[i].checked = 0; }
    8.    else {document.trips.elements[i].checked = 1;}
    9.    }
    10.   }
    11.   
    12.   function UncheckAll(){
    13.   count = document.trips.elements.length;
    14.    for (i=0; i < count; i++)
    15.    {
    16.    if(document.trips.elements[i].checked == 1)
    17.    {document.trips.elements[i].checked = 0; }
    18.    else {document.trips.elements[i].checked = 1;}
    19.    }
    20.   }
    21.   
    22.   <input onclick="CheckAll()"> <input onclick="UncheckAll()">
Comments


Unecessary

Posted by: duniyadnd around 17 Months Ago
Line: 6

Line 6 and 7 are not needed at all for CheckAll(). At the moment, it's going to alternate the status of the checkbox.

Same goes for UncheckAll(). By default you should have it all going to .checked == 1 / .checked == 0