views:

281

answers:

2

i have got a HTML table which contain a check box in and other checkboxes in i want that all 's checkboxes should be checked /unchecked on basis of 's Check box... please help me resolve this... i wrote follwoing code. but its not working..

   <script language="javascript" type="text/javascript">
  function SelectAll(id) {

    var frm = document.getElementById('tblemail');

    for (i=0;i<frm.elements.length;i++) {

        if (frm.elements[i].type == "checkbox") {

            frm.elements[i].checked = document.getElementById(id).checked;

        }

    }

} 

</script>
+2  A: 

Try the following code

var frm = document.getElementById('tblemail').getElementsByTagName("input");
var len = frm.length;
var checkedStatus = document.getElementById(id).checked;

for (i=0;i<len;i++) 
{
    if (frm[i].type === "checkbox") 
    {    
        frm[i].checked = checkedStatus;
    }
}
rahul
I would consider avoiding looking up the `getElementById` inside the loop. `var checked = document.getElementById(id).checked; for (/* blah blah*/) { frm[i].checked = checked; }`
nickf
Edited my code.
rahul
A: 

Have you Tried

document.getElementById(id).checked = true 
Gerard Banasig