Check all checkboxes using javascript | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Check all checkboxes using javascript

Saturday

Javascript Check and Uncheck All Checkboxes:
This is a simple script to check all check boxes in a form using javascript.
You can check all the check boxes inside the table by clicking another one check box placed in header of that table.
In that same way you can uncheck all checkboxes.If you unchecking a single check box from inside the table then the header(main) check box is automatically go to unchecked state.


Code:
<html>
    
<head>
        
<title>JavaScript - Select All checkbox in form</title>
        
<style type="text/css">
            table
.tblCheckDemo {
                font-family
:arial;
                border-collapse
:collapse;
                border
: solid 3px #7f7f7f;
                font-size
:small;
            
}

            table
.tblCheckDemo td {
                padding
: 5px;
                border-right
: solid 1px #7f7f7f;
            
}

            table
.tblCheckDemo .even {
                background-color
: #EEE8AC;
            
}

            table
.tblCheckDemo .odd {
                background-color
: #F9FAD0;
            
}

            table
.tblCheckDemo th {
                border
: 1px solid #7f7f7f;
                padding
: 5px;
                height
: auto;
                background
: #D0B389;
            
}
        </
style>

        
<script LANGUAGE="JavaScript">

            
function checkMain()
             {               
               if(document.myform.chkMain.checked==true)
                {
                       checkAll()

                }  
             else
                {
                    uncheckAll()

                 }
            }
            
function checkSub()
            {
                fruit 
= document.myform.chkFruit;
                var count 0;
                for (i 0i < fruit.lengthi++)
                    
if(fruit[i].checked) 
                      {
                    count++;
                
}

                
if(fruit.length == count) 
                {                     
                     document.myform.chkMain.checked = true;
                
}
                
else
                
{
                    
document.myform.chkMain.checked = false;
                
}
            }

            
function checkAll()
            {

                fruit = document.myform.chkFruit
                for 
(i 0i < fruit.lengthi++)
                    fruit[i].checked = true ;
            
}

            
function uncheckAll()
            {

                fruit = document.myform.chkFruit;
                for 
(i 0i < fruit.lengthi++)
                fruit[i].checked
= false ;
            
}
        
</script>
   
</head>
   
<body>
       
<form name="myform" method="post">
           
<b>Select Your Favorite Fruits </b><br>
           
<table class="tblCheckDemo">
               
<tr>
                   
<th>S.NO</th>
                   
<th>Fruit</th>
 
<th> <input type="checkbox" name="chkMain" onClick="checkMain()"></th>
               
</tr>
               
<tr class="even">
                   
<td>1</td>
                   
<td>Apple</td>
 
<td> <input type="checkbox" name="chkFruit" value="1" onClick="checkSub()"></td>
               
</tr>
               
<tr class="odd">
                   
<td>2</td>
                   
<td>Orange</td>                    <td> <input type="checkbox" name="chkFruit" value="2" onClick="checkSub()"></td>
               
</tr>
                         .
                         .
                         . 
                         .
   
            
</table>
       
</form>
   
</body>
</html>
Demo:

Select Your Favorite Fruits


S.NO Fruit
1 Apple
2 Orange
3 Asian palmyra palm
4 Goe
5 Garden strawberry
6 Sapote
7 Jackfruit
8 Sour cherry
9 Palmera de coco
10 Night blooming cereus

0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets