Solved

Dynamically adding table row with a checkbox using javascript

Posted on 2008-10-21
3
5,774 Views
Last Modified: 2013-11-11
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
Comment
Question by:chuck620
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22771468
You could try the following. I hope it helps.

Kind regards,

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

Open in new window

0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 125 total points
ID: 22773071
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
 

Author Closing Comment

by:chuck620
ID: 31508703
Thanks for the quick response.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question