Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting the colspan attribute of a "th" cell dynamically

Posted on 2007-04-09
3
Medium Priority
?
791 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

610 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