473,661 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ 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=javasc ript>
<!--

function switchRow() {
var table = document.getEle mentById("test" )
var oRows = table.getElemen tsByTagName("TR ");
var oTDs;
var numix;
var newTR = document.create Element("TR");

var NumRowA = 1;
var NumRowB = 2;

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

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

//-->
</SCRIPT>

</HEAD>
<BODY>

<button onclick="switch Row();">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 1855
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(oC hild1, oChild2) {
var oParent1 = oChild1.parentN ode,
oParent2 = oChild2.parentN ode,
oSib1 = oChild1.nextSib ling,
oSib2 = oChild2.nextSib ling;
if (oSib2 != oChild1) {
oParent2.insert Before(oChild1, oSib2);
}
if (oSib1 != oChild2) {
oParent1.insert Before(oChild2, oSib1);
}
}

Using that, your function switchRow() could be as short as

function switchRow(nRow1 , nRow2) {
var oTable, aRows;
if (document.getEl ementById
&& (oTable = document.getEle mentById("test" ))
&& (aRows = oTable.rows)
&& aRows[nRow1]
&& aRows[nRow2]
) {
swapChildren(aR ows[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=javasc ript>
<!--

function switchRow() {
var table = document.getEle mentById("test" )
var oRows = table.getElemen tsByTagName("TR ");
var oTDs;
var numix;
var newTR = document.create Element("TR");

var NumRowA = 1;
var NumRowB = 2;

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

for (numix = 0; numix < oTDs.length; numix++) {
var newTD = document.create Element("TD");
newTD.innerHTML = oTDs[numix].innerHTML;
newTD.className = oTDs[numix].className;
newTR.insertBef ore(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
5364
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 if the file has a TXT extension. If this is the case, what Javascript could I write which would take the file, copy it to a temporary area and rename it as a TXT file. It should then delete the file when I select a new file or close the window....
2
7116
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 this. The part I'm concerned with is the cell FFFFF which contains a multiple <SELECT> list of <OPTION>s. As you can see the first two rows span more than one column (if this has any importance). What I want is to constrain FFFFF not to exceed a...
2
7919
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 single script statement that will apply a given padding to all elements of a given class, or put a border around all the LIs on a page? -- Harlan Messinger Remove the first dot from my e-mail address. Veuillez ôter le premier point de mon adresse...
4
5501
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 browser (Mozilla) as raised with a slight shadow to the right and below. The borders are specified with the "border" attribute in the <table> tag: <table border cellpadding=2 width="100%"> and not via a style sheet. If I add a DOCTYPE...
9
2950
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
4029
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 stored procedure updates a row in C, adds a row each in B & C. I get an integrity violation. All the foreign keys are deferrable, and the stored procedure is called from within a transaction with constraints deferred. (And the foreign keys do refer to...
5
1720
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 CSV file while removing duplicate records. The main problem is that the script currently takes about 20 seconds to execute, and were it to take much longer it would time out. Below is the script itself, and then some example lines from the log...
4
10384
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
16958
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 examples I found on the web. Works in IE and mozilla. http://www.imaputz.com/cssStuff/bigFourVersion.html
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.