Solved

Setting the colspan attribute of a "th" cell dynamically

Posted on 2007-04-09
3
767 Views
Last Modified: 2012-08-13
Hi,

I am dynamically adding rows to an HTML table and one of them is a "th" row. I wanted to set the "colspan" attribute for this dynamically and am not able to do so - for some reason the code below does everything except increase the colspan of the row. My code is below. Please help.

      var tBody = document.getElementById("Agenda").getElementsByTagName("tBody")[0];
      var tRowH = document.createElement("tr");
      var tHead = document.createElement("th");
      tHead.setAttribute("colspan","2");
      tHead.setAttribute("bgcolor","#cccc99");
      tHead.setAttribute("align","center");
      tHead.innerHTML = "Topic 3";      
      tBody.appendChild(tRowH);
Thanks,
Aruna
0
Comment
Question by:arunatata
3 Comments
 
LVL 3

Expert Comment

by:mtorre615
ID: 18879598
try tHead.colSpan = 2.
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18879682
There's a quirk to this one.  Use appendChild to attach the TH to the TR tag first, and THEN set the colSpan attribute.
0
 
LVL 8

Accepted Solution

by:
jawahar_prasad earned 500 total points
ID: 18880913
Hi.. Aruna,
TRY THIS.. Some modifications to your code...

     var tBody = document.getElementById("Agenda").getElementsByTagName("tBody")[0];
      var tRowH = document.createElement("tr");
      var tHead = document.createElement("th");
      tHead.setAttribute("colSpan","2");
      tHead.setAttribute("bgColor","#cccc99");
      tHead.setAttribute("align","center");
      tHead.innerHTML = "Topic 3";      
      tBody.appendChild(tRowH);

EXPLANATION:
Colspan should be "colSpan" and not "colspan"    //CASE SENSITIVE
bgcolor should be "bgColor" and not "bgcolor"      //CASE SENSITIVE

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 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