Solved

Dynamically adding table row with a checkbox using javascript

Posted on 2008-10-21
3
5,659 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

679 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