473,473 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DOM table manipulation not consistent in IE vs Mozilla

hi,

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.

The sample code works as it should in IE, and switches the two rows
nicely.
In Mozilla however it fails the first time, but for some odd reason it
works fine afterwards.
Mozilla doesn't seem to be removing the duplicated node the first time,
which makes it appear as if an extra node is inserted into the table
(which is actualy the case), but on subsequent attempts, it seems to be
removing the duplicated node perfectly...

So why not the first time?

Any help would be appreciated.

regards,
J.

--- start code ---
<HTML>
<HEAD>

<SCRIPT LANGUAGE=javascript>
<!--

function switchRow() {
var table = document.getElementById("test")
var oRows = table.getElementsByTagName("TR");
var oTDs;
var numix;
var newTR = document.createElement("TR");

var NumRowA = 1;
var NumRowB = 2;

oTDs = oRows[NumRowB].getElementsByTagName("TD");

for (numix = 0; numix < oTDs.length; numix++) {
var newTD = document.createElement("TD");
newTD.innerHTML = oTDs[numix].innerHTML;
newTD.className = oTDs[numix].className;
newTR.insertBefore(newTD, null);
}
// using table object directly doesn't work on IE (o_O)
// but this indirect approach does the trick...
oRows[NumRowA].parentNode.insertBefore (newTR, oRows[NumRowA]);
// remove node
oRows[NumRowB].parentNode.removeChild(oRows[NumRowB].nextSibling,
true);
}

//-->
</SCRIPT>

</HEAD>
<BODY>

<button onclick="switchRow();">switch</button>

<table border="1" id="test">
<tr>
<td>one</td>
<td>nitron</td>
</tr>
<tr>
<td>two</td>
<td>soldiers</td>
</tr>
<tr>
<td>three</td>
<td>analysis</td>
</tr>
<tr>
<td>four</td>
<td>dark heart</td>
</tr>
</table>

</BODY>
</HTML>
--- end code

Jul 23 '05 #1
3 1841
J. Baute wrote:
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.


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
Jul 23 '05 #2
J. Baute wrote:
hi,

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.
[...]
Any help would be appreciated.

regards,
J.

--- start code ---
<HTML>
<HEAD>

<SCRIPT LANGUAGE=javascript>
<!--

function switchRow() {
var table = document.getElementById("test")
var oRows = table.getElementsByTagName("TR");
var oTDs;
var numix;
var newTR = document.createElement("TR");

var NumRowA = 1;
var NumRowB = 2;

oTDs = oRows[NumRowB].getElementsByTagName("TD");

for (numix = 0; numix < oTDs.length; numix++) {
var newTD = document.createElement("TD");
newTD.innerHTML = oTDs[numix].innerHTML;
newTD.className = oTDs[numix].className;
newTR.insertBefore(newTD, null);
}
// using table object directly doesn't work on IE (o_O)
// but this indirect approach does the trick...


Note that in IE you need to add new rows to the tbody, not directly
to the table. Even if you don't create a tbody element or code one
in your HTML, browsers will insert one where they feel appropriate
(the HTML spec says tables must have a tbody)..

When appending rows to a table, Gecko browsers seem infer that you
really meant to add it to the tbody, but IE doesn't. So add rows to
the tbody and everyone's happy.

A simple fix is to code a tbody element in your HTML and put the
table id on that rather than the table tag. You can also have
multiple tbodys, so you can add rows to different parts of your table
without row counting or other hacks.

;-p

[...]

--
Rob
Jul 23 '05 #3
thanx,
works like a charm!

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Froggy_Jo | last post by:
I have a split screen HTML display. In the bottom half, I have a series of file names with hyperlinks. In the top half, I want to display the file selected. However, it seems I can only do this...
2
by: LC's No-Spam Newsreading account | last post by:
I have a form arranged in a table (you can see an example in the page http://cosmos.mi.iasf.cnr.it/~lssadmin/Website/LSS/Help/query.html) The table is on three columns but has a structure like...
2
by: Harlan Messinger | last post by:
I know how to change attributes of a given element using Javascript, but can you change an attribute for a CSS class with script? Or for a particular kind of tag? For example, can you write a...
4
by: David Ross | last post by:
In my <http://www.rossde.com/inflation.html>, I have a small table in the middle of the page. This page is composed without a DOCTYPE statement at the beginning. The table borders appear to my...
9
by: Adelson Anton | last post by:
I need to iterate characters in a loop. For example, all the letter from a to r. In C I would just do that: char c; for (c='a'; c<='r'; c++) It doesn't work in JS. Thanks.
4
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
5
by: Cleverbum | last post by:
I'm not really accustomed to string manipulation and so I was wondering if any of you could be any help i speeding up this script intended to change the format of some saved log information into a...
4
by: Gabriel | last post by:
Which is the mistake? I need one column with different size. Thanks a lot. table { border: outset 2pt; border-collapse: separate; border-spacing: 4pt; background:black;
1
by: since | last post by:
I figured I would post my solution to the following. Resizable column tables. Search and replace values in a table. (IE only) Scrollable tables. Sortable tables. It is based on a lot...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.