How to check and unselect all with single checkbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azegurb
    New Member
    • Sep 2009
    • 32

    How to check and unselect all with single checkbox?

    hi i have created function that automatically adds rows with chekcboxes. i can create two button that one serves checking all checkboxes and the another one serves for unchecking rows. but i would like to check/uncheck all checkboxes with single checkbox.
    then i have created checkbox for the purpose to check or uncheck all checkboxes. but it neigther checks nor unchecks all
    here is script
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    var a=0;
    function add(){
    	a++;
    	
    	
    	var cedvel=document.getElementById("tab");
    	
    	
    	if(a>cedvel.rows.length){
    		
    		a=cedvel.rows.length;
    		
    		}
    		
    	
    		
    	var sira=cedvel.insertRow(a);
    	
    	var isare=sira.insertCell(0);
    	var solteref=sira.insertCell(1);
    	var sagteref=sira.insertCell(2);
    	
    	var birinci=document.createElement('input');
    	birinci.type='checkbox';
    	isare.appendChild(birinci);
    	
    	solteref.innerHTML=a+1;
    	
    	var ucuncu=document.createElement('input');
    	ucuncu.type="text";
    	ucuncu.size="11";
    	sagteref.appendChild(ucuncu);
    	
    	}
    
    function del(){
    	var cedvel=document.getElementById("tab");
    	var a=cedvel.rows.length;
    	for(i=0; i<a; i++){
    		
    		var komp=cedvel.rows[i].cells[0].childNodes[0];
    		if(komp.checked==true){
    			
    			cedvel.deleteRow(i);
    			a--;
    			i--;
    			
    			}
    		if(i>=0)
    		cedvel.rows[i].cells[1].childNodes[0].nodeValue=i+1
    		}
    	
    }
    
    function checkall(){
    	var sec=document.getElementById("tab");
    	for(i=0; i<sec.rows.length; i++){
    		
    		sec.rows[i].cells[0].childNodes[0].checked=true;
    		
    		}
    	
    	
    	
    	}
    
    function uncheckall(){
    var sec=document.getElementById("tab");
    	for(i=0; i<sec.rows.length; i++){
    		
    		sec.rows[i].cells[0].childNodes[0].checked=false;
    		
    		}
    	
    }
    var checked=1;
    function all(){
    	var sec=document.getElementById("tab");
    	for(i=0; i<sec.rows.length; i++){
    		if(checked==1){
    		sec.rows[i].cells[0].childNodes[0].checked=true;
    		checked=2;
    		}
    		if(checked==2){
    		sec.rows[i].cells[0].childNodes[0].checked=false;
    		checked=1;	
    			
    		}
    		}
    	
    	
    	
    	}
    
    
    
    
    
    </script>
    
    	
    
    
    
    
    
    </head>
    
    <body bgcolor="#999999">
    &nbsp<input type="checkbox" onclick="all()"  value="check/uncheckall" id="box"/>check/uncheck all
    <table  id="tab" style="font-family:'Comic Sans MS', cursive">
    <tr><td><input type="checkbox" /></td><td>1</td><td><input type="text" size="11" /></td></tr>
    </table>
    <input type="button" value="add" onclick="add()"  style="font-family:'Comic Sans MS', cursive"/><br />
    <input type="button" value="delete" onclick="del()" style="font-family:'Comic Sans MS', cursive"/>
    <input type="button" value="uncheckall" onclick="uncheckall()" style="font-family:'Comic Sans MS', cursive" /><br />
    <input type="button" value="checkall" onclick="checkall()" style="font-family:'Comic Sans MS', cursive" /><br />
    
    
    </body>
    </html>
    Thanks in advance for attention
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hi azegurb,
    I went through your code. Some small changes will make ur code work.

    In HTML

    Code:
    <input type="checkbox" onclick="all1(this)"  value="check/uncheckall" id="box"/>
    please change the function name and add a parameter. I doubt there is some predefined function all() in JS. Because when I tried with all() it was not working.

    In JS,

    Code:
    function all1(ths){
      if(ths.checked==true)
         checkall();
      else
        uncheckall();
    }
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • Bharat383
      New Member
      • Aug 2011
      • 93

      #3
      Code:
      <input type="checkbox" name="checkall" id="checkall" onclick="check_all()">Select All
      <br>
      
      <input type="checkbox" name="check_0" id="check_0">Option 1
      <input type="checkbox" name="check_1" id="check_1">Option 2
      <input type="checkbox" name="check_2" id="check_2">Option 3
      <input type="checkbox" name="check_3" id="check_3">Option 4
      <input type="checkbox" name="check_4" id="check_4">Option 5
      <input type="checkbox" name="check_5" id="check_5">Option 6
      <input type="checkbox" name="check_6" id="check_6">Option 7
      <input type="checkbox" name="check_7" id="check_7">Option 8
      <input type="checkbox" name="check_8" id="check_8">Option 9
      <input type="checkbox" name="check_9" id="check_9">Option 10
      
      <br><hr>
      
      <script type="text/javascript">
      function check_all()
      {
      
      	for(i=0;i<10;i++)
      	{
      		tmp_checkbox_id = "check_"+i;
      			if(document.getElementById("checkall").checked == true)
      			{	
      								document.getElementById(tmp_checkbox_id).checked = true;
      			}
      			else
      			{	
      				document.getElementById(tmp_checkbox_id).checked = false;
      			}
      
      	}
      }
      </script>
      Last edited by acoder; May 23 '12, 10:37 PM. Reason: Please post your code using [code] tags and explain your solution

      Comment

      Working...