Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically adding table row with a checkbox using javascript

Posted on 2008-10-21
3
Medium Priority
?
5,866 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

688 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