Need help with javascript unchecking of one checkbox unchecks the main checkbox -


i have table has header checkbox in row. data gets dynamically added in table javascript. checking on checkbox in header row checks or unchecks checkboxes of rows.. have done till here in method "checkuncheck()", i.e.:

<input type="checkbox" id="chkappid" onclick="checkuncheck()"/> 

but now, suppose there 10 records dynamically added in table. have checked main checkbox in header. after that, uncheck 1 of checkboxes, e.g.: on 10th row, checkbox of header must unchecked....and if check of 10 rows of checkboxes manually, checkbox of main header must checked. observed in yahoo, gmail, etc...

one way count number of checked ones , compare total number of rows, , if found equal, check main one; otherwise, uncheck main one. can't figure out that... not working!

update

<html> <head><title>checkbox puzzle</title></head>  <body>     <input type="checkbox" id="chkappid" onclick="checkuncheck()"/>main<br/>     <input type="checkbox" id="chkappid1" onclick="check()"/>chk1<br/>     <input type="checkbox" id="chkappid2" onclick="check()"/>chk2<br/>     <input type="checkbox" id="chkappid3" onclick="check()"/>chk3<br/>    <script type="text/javascript">     function checkuncheck()     {         var totalrows = 4;         if(document.getelementbyid("chkappid").checked){             checkedall = true;         }         else{             checkedall = false;         }          for(i = 1; i< totalrows; i++){             document.getelementbyid("chkappid"+i).checked = checkedall;         }     }       function check()     {         var totalrows = 4,count=0;         for(i = 1; i< totalrows; i++)   {             if(document.getelementbyid("chkappid"+i).checked){                 count= count+1;             }         }         if(count ==totalrows - 1){             //alert("check main");             document.getelementbyid("chkappid").checked = true;         }         else         {             //alert("uncheck main");             document.getelementbyid("chkappid").checked = false;         }     }   </script> </body> </html> 

this simpler version can explain purpose, else may need implement such things.

when dynamically insert checkboxes, give them each different ids?

post code have , may able you.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -