0

I have got a HTML table which contain a check box in <TH> and other checkboxes in <TD> I want that all <TD>'s checkboxes should be checked /unchecked on basis of <TH>'s Check box; please help me resolve this. I wrote following 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 Answers 2

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would consider avoiding looking up the getElementById inside the loop. var checked = document.getElementById(id).checked; for (/* blah blah*/) { frm[i].checked = checked; }
0

Have you Tried

document.getElementById(id).checked = true 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.