Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6043
  • Last Modified:

Dynamically adding table row with a checkbox using javascript

I have a table that I am dynamically adding rows to using javascript, in this row I need to add a cell with a checkbox control.  I'm able to create the checkbox fine but I cannot figure out how to make it checked when it's rendered.  Here's the code I have so far:

var cell0 = row.insertCell(0);
var chkBox = document.createElement('input');
chkBox.type='checkbox';
chkBox.name='tnCheckbox' + iteration;
chkBox.id='tnCheckbox' + iteration;
chkBox.checked=true;
cell0.appendChild(chkBox);

Like I said, I'm able to create the control fine, I just need it to be checked by default.  

Thanks in advance.
0
chuck620
Asked:
chuck620
1 Solution
 
MatthiasVanceCommented:
You could try the following. I hope it helps.

Kind regards,

Matthias Vance
chkBox.setAttribute("checked", "checked");

Open in new window

0
 
azadisaryevCommented:
try chkBox.checked='checked'; instead.

if that does not work, consider setting the checked attribute AFTER the checkbox is rendered, i.e.:
...
chkBox.id='tnCheckbox' + iteration;
//chkBox.checked=true;
cell0.appendChild(chkBox);
document.getElementById('tnCheckbox' + iteration).checked=true;
0
 
chuck620Author Commented:
Thanks for the quick response.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now