Solved

Setting the colspan attribute of a "th" cell dynamically

Posted on 2007-04-09
3
777 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
[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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

717 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