473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with function insertBefore()

This is partly a repost that was maybe too buried to be seen. Sorry for
that. I thought it would be an easy to be answered question, but I may be
wrong...

I wanted to _insert_ cloned table row into the same table, and while using
appendChild() worked fine insertBefore() - which is what I actually need -
resulted in an error.

This code works...

active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true)
document.getElementById ("Table").appendChild (cloned_row)

But trying to insert it before active_row with this code failed[*]...

active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true)
document.getElementById ("Table").insertBefore (cloned_row, active_row)

What's wrong with that code? Any insights greatly appreciated!

Janis
[*] The error messages in the Javascript console:

Error: uncaught exception: [Exception... "Node was not found"
code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)"
location: "file:... Line: 390"]
Dec 22 '06 #1
2 8965
Janis Papanagnou wrote:
This is partly a repost that was maybe too buried to be seen. Sorry for
that. I thought it would be an easy to be answered question, but I may be
wrong...

I wanted to _insert_ cloned table row into the same table, and while using
appendChild() worked fine insertBefore() - which is what I actually need -
resulted in an error.

This code works...

active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true)
document.getElementById ("Table").appendChild (cloned_row)
That won't work in IE because it wants to append rows to a tbody, not a
table. Also, you need to change the id of the cloned row otherwise
you'll have duplicate IDs, which is invalid HTML.

var active_row = document.getElementById("Active_Row");
var cloned_row = active_row.cloneNode(true);
cloned_row.id = '';
active_row.parentNode.appendChild(cloned_row);

Opinions differ on the use of semi-colons to end statements, however I
think it's much better to always use them. It's also better to not put
a space between an identifier and an opening bracket '(', and always
use var to keep variables local unless you have a good reason not to
use it.

But trying to insert it before active_row with this code failed[*]...

active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true)
document.getElementById ("Table").insertBefore (cloned_row, active_row)

What's wrong with that code? Any insights greatly appreciated!
Try:

var active_row = document.getElementById("Active_Row");
var cloned_row = active_row.cloneNode(true);
cloned_row.id = '';
active_row.parentNode.insertBefore(cloned_row, active_row);
--
Rob

Dec 22 '06 #2
RobG wrote:
Janis Papanagnou wrote:
> active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true)
document.getElementById ("Table").appendChild (cloned_row)

That won't work in IE because it wants to append rows to a tbody,
I have to admit I don't understand why. But okay, I'll avoid that.
not a
table. Also, you need to change the id of the cloned row otherwise
you'll have duplicate IDs, which is invalid HTML.
Yes, that's what I feared. (I mentioned that in the other thread.)
var active_row = document.getElementById("Active_Row");
var cloned_row = active_row.cloneNode(true);
cloned_row.id = '';
active_row.parentNode.appendChild(cloned_row);

Opinions differ on the use of semi-colons to end statements, however I
think it's much better to always use them. It's also better to not put
a space between an identifier and an opening bracket '(', and always
use var to keep variables local unless you have a good reason not to
use it.
Thanks, I'll keep that in mind.
>>But trying to insert it before active_row with this code failed[*]...

active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true)
document.getElementById ("Table").insertBefore (cloned_row, active_row)

What's wrong with that code? Any insights greatly appreciated!

Try:

var active_row = document.getElementById("Active_Row");
var cloned_row = active_row.cloneNode(true);
cloned_row.id = '';
active_row.parentNode.insertBefore(cloned_row, active_row);
Clearing 'id' and using 'parentNode' indeed fixed the problems I had.

That was very helpful advice; thanks a lot! :-)

Janis
Dec 22 '06 #3

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

Similar topics

3
2820
by: Rutger Claes | last post by:
Code and problem are from PHP5: When I execute the following piece of code, the DomException is thrown with a message: Not Found Exception. I assume this means that the node I extracted from the...
1
7284
by: Michael J Whitmore | last post by:
I am getting tired of losing hair over this. Here is a function that simply inserts one of three images into a document right before printing. It is called for every element that has a specific...
1
20674
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
1
1537
by: Nicole Schenk | last post by:
Client-side problem scripting problem. I have a table of about 300 rows with many columns. User chooses to sort on one column. Sort is very fast (I do this by extracting the column data along with...
5
3345
by: Derek Basch | last post by:
Can anyone tell me why I cant insertBefore() objects that are selected using getElementById()? <html> <head> </head> <body> <p id="heading1"></p>
4
6098
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on...
13
11783
by: db | last post by:
Hi@all, I am always thinking that there is no problems that I can not solve. But now i changed my idea...... And I have posted it in javascript group, since i think both javascript and php...
2
7517
by: ashishda | last post by:
I am using a AJAX script to retrieve XML data and then append new <select> options in my table. I see that the appendChild() works fine, but the insertBefore() fails. If however, I have both...
0
7138
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...
0
7355
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
7510
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...
0
4737
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...
0
3225
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
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.