W3C home > Mailing lists > Public > www-dom@w3.org > October to December 1999

Rearranging rows in a table

From: Jeff Fink <jfink@ryos.com>
Date: Wed, 13 Oct 1999 13:02:26 -0400 (EDT)
To: <www-dom@w3.org>
Message-ID: <002c01bf159c$928bf330$868d2dc7@meesh.ryos.com>

I have this table:
<TABLE ID="tbl">
		<TH>Col 0</TH>

		<TD>Row 0</TD>
		<TD>Row 1</TD>

I want to switch the order of the rows in the table body and there does not
seem to be a straightforward way to do this. I can write this code:

	var pTable = document.getElementById("tbl");
	var pRow0 = pTable.tBodies[0].rows[0];
	var pRow1 = pTable.tBodies[0].rows[1];

	var pNewRow = pTable.tBodies[0].insertRow(0);
	var pCell, i, j;

	for(i = 0; i < pRow1.cells.length; i++)
		pCell = pNewRow.insertCell(i);
		for(j = 0; j < pRow1.cells[i].childNodes.length; j++)


However, if I've set any attributes on the HTMLTableRowElement or on the
HTMLTableCellElement via setAttribute(), they get lost because I can't clone
these elements and insert them into the new table.  Why aren't there
insertRow() and insertCell() methods that allow you to pass the node you
want for the new Row/Cell?  If these methods were in place, this problem
would become:

	var pTable = document.getElementById("tbl");
	var pRow1 = pTable.tBodies[0].rows[1];

	pTable.tBodies[0].insertRow(pRow1, 0);

And of course, all my attributes get moved over because I'm moving a single
node rather than copying everything.
Received on Thursday, 14 October 1999 10:16:27 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 20 October 2015 10:46:06 UTC