Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Browsers / Internet Explorer / July 2004



Tip: Looking for answers? Try searching our database.

Dynamically Changing TD cells into TH cells

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason Butera - 26 Jul 2004 03:12 GMT
I want to be able to dynamically turn my first row TD cells into TH
cells. I know that I can't change the outerHTML property, so I simply
create a new table row and use appendChild to insert TH cells, and
then remove the existing row. However, I want to make sure that the TH
cells retain any/all attributes of the original TD cell. This is where
I'm stumped. I thought the following would work:

// BEGIN CODE
//---------------

var oldRow = myTable.rows[0];
var newRow = myTable.insertRow[0];
var newCell;

// LOOP THE ORIG. ROW AND RECREATE ANOTHER USING TH CELLS
for (i=0; i < oldRow.cells.length; i++) {

 // CREATE THE TH ELEMENT USING AN HTML STRING MODELED FROM ORIG.
CELL
 var s = oldRow.cells[i].outerHTML;    // s = "<TD colspan=2></TD>"
 s = s.replace(/<TD/gi, "<TH");        // s = "<TH colspan=2></TD>"
 s = s.replace(/<\/TD>/gi, "<\/TH>");  // s = "<TH colspan=2></TH>"

 newCell = document.createElement(s);

 // DEBUG: newCell.outerHTML
 // SHOWS -- "<<TH colspan=2>asdf</TH>><<TH colspan=2>asdf</TH>>" --
WHY??????
 alert(newCell.outerHTML);

 // INSERT THE TH CELL
 newRow.appendChild(newCell);

}

// REMOVE THE OLD ROW
myTable.deleteRow[1];

//---------------
// END CODE

This code sort of works, HOWEVER, the HTML that is appended to newRow
is ALL MESSED UP! In debugging, I check the outerHTML of newCell after
it is created and it looks like "<<TH colspan=2>asdf</TH>><<TH
colspan=2>asdf</TH>>" when it should look like "<TH
colspan=2>asdf</TH>". What is going on here? Any ideas?

Jason
Stan Scott - 26 Jul 2004 08:21 GMT
Jason,

I'd suggest that, instead of changing the actual tags dynamically, you
structure your table like this:

<table id="myTable">
<thead id="myHead">
 <tr>
  <td>First</td><td>Second</td><td>Third</td>
 <tr>
</thead>
<tbody>
   <tr>  and so on

Once you've done this, you can set the top row of your table to
bold,centered, simply by using this code:

document.getElementById("myHead").style.fontWeight="bold"
document.getElementById("myHead").style.textAlign="center"

When you want to "deactivate" the heading styles, just set the tHead style
attributes to "".  Setting up a class would be even more efficient.

You could, I suppose, change the innerHTML property of myHead to something
else, but it seems easier to do it the way I've described.

Stan Scott
New York City

> I want to be able to dynamically turn my first row TD cells into TH
> cells. I know that I can't change the outerHTML property, so I simply
[quoted text clipped - 44 lines]
>
> Jason
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.