| re: DOM table manipulation not consistent in IE vs Mozilla
J. Baute wrote:
[color=blue]
> I'm trying to write some javascript code that will swap two rows in a
> table, in an attempt to provide users with an easy and visual way to
> manually change the order of the listed items in a table.[/color]
To swap two nodes in the DOM tree, you don't have to create a third one.
I always use something like:
function swapChildren(oChild1, oChild2) {
var oParent1 = oChild1.parentNode,
oParent2 = oChild2.parentNode,
oSib1 = oChild1.nextSibling,
oSib2 = oChild2.nextSibling;
if (oSib2 != oChild1) {
oParent2.insertBefore(oChild1, oSib2);
}
if (oSib1 != oChild2) {
oParent1.insertBefore(oChild2, oSib1);
}
}
Using that, your function switchRow() could be as short as
function switchRow(nRow1, nRow2) {
var oTable, aRows;
if (document.getElementById
&& (oTable = document.getElementById("test"))
&& (aRows = oTable.rows)
&& aRows[nRow1]
&& aRows[nRow2]
) {
swapChildren(aRows[nRow1], aRows[nRow2]);
}
}
ciao, dhgm |