0

I have a table containing checkboxes. I add checkboxes to a table whenever a button is clicked as follows:

    var cell3 = row.insertCell(2);
    cell3.innerHTML = '<input type="checkBox" value=\"selected?\" style="cursor:pointer" value="htcb"/>';

How can I find out if the checkbox in, say cell 3, is selected?

I've tried this

var myCheckBox = row.cells[2].innerHTML;
   if(myCheckBox .checked == true)
      //

but it doesn't work

2
  • 3
    Why should higherTierCB point to your checkbox? Is there code missing? Commented Oct 22, 2012 at 9:22
  • No worries. And to be browser compatible it would be wise to use a library like jQuery for creating and checking dom elements. Commented Oct 22, 2012 at 9:25

2 Answers 2

1

The innerHTML property is just a string, you need to access the DOM object itself.

Seeing as the <input> is the only thing in the cell, this should work:

var myCheckBox = row.cells[2].firstChild;
if (myCheckBox.checked) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

js function:

function check() {
     if($(this).is(”:checked”))
     {
         alert(’checked’);
     }
}

you must add onchange listener:

cell3.innerHTML = '<input type="checkBox" value=\"selected?\" 
               style="cursor:pointer"value="htcb" onchange="check()"/>';

1 Comment

There is no mentioning of jQuery

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.