better way of swapping columns and rows in a table (code provided)

F

foldface

Hi
This works but is a bit long-winded. Is there a better way?
ta
F


<style>
..Header
{
background-color: gray;
}

</style>

<table id=theTable>
<tr>
<td class=Header nowrap style="bob: 1">Header1</td>
<td class=Header>Header2</td>
<td class=Header>Header3</td>
<td class=Header>Header4</td>
<td class=Header>Header5</td>
</tr>
<tr>
<td> Contents01 </td>
<td> Contents02 </td>
<td> Contents03 </td>
<td> Contents04 </td>
<td> Contents05 </td>
</tr>
<tr>
<td> Contents11 </td>
<td> Contents12 </td>
<td> Contents13 </td>
<td> Contents14 </td>
<td> Contents15 </td>
</tr>
</table>

<hr>
<input type=button value=PressMe onclick="func()">

<script>

function func()
{
var theTable = document.getElementById('theTable')
var theTable2 = document.createElement("table");

var numRows = theTable.rows.length;
var numCells = theTable.rows[0].cells.length;

for(var i=0; i<numCells; i++)
{
var newRow = theTable2.insertRow(-1)
for(var j=0; j<numRows; j++)
{
var cell = theTable.rows[j].cells;
var newCell = newRow.insertCell(-1);
newCell.innerHTML = cell.innerHTML;
newCell.className = cell.className;
newCell.style.cssText = cell.style.cssText
}
}

while (theTable.rows.length > 0)
{
theTable.deleteRow(0);
}

var numRows = theTable2.rows.length;
var numCells = theTable2.rows[0].cells.length;
debugger;
for(var i=0; i<numRows; i++)
{
var newRow = theTable.insertRow(-1)
for(var j=0; j<numCells; j++)
{

var cell = theTable2.rows.cells[j];
var newCell = newRow.insertCell(-1);
newCell.innerHTML = cell.innerHTML;
newCell.className = cell.className;
newCell.style.cssText = cell.style.cssText
}
}

while (theTable2.rows.length > 0)
{
theTable2.deleteRow(0);
}
}
</script>
 
F

foldface

..and where the heck did style class 'bob: 1'
come from.. You should validate your CSS, rather
than (cough)test(cough) it in IE.

I think most people would recognise that as the equivilent of
hello world and not bother posting about it
(..and that is without even getting to the JS)

which naturally on comp.lang.javascript is the only bit I care about
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top