473,804 Members | 3,031 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.getEle mentById ("Active_Row ")
cloned_row = active_row.clon eNode (true)
document.getEle mentById ("Table").appen dChild (cloned_row)

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

active_row = document.getEle mentById ("Active_Row ")
cloned_row = active_row.clon eNode (true)
document.getEle mentById ("Table").inser tBefore (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_N OT_FOUND_ERR)"
location: "file:... Line: 390"]
Dec 22 '06 #1
2 8980
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.getEle mentById ("Active_Row ")
cloned_row = active_row.clon eNode (true)
document.getEle mentById ("Table").appen dChild (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.getEle mentById("Activ e_Row");
var cloned_row = active_row.clon eNode(true);
cloned_row.id = '';
active_row.pare ntNode.appendCh ild(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.getEle mentById ("Active_Row ")
cloned_row = active_row.clon eNode (true)
document.getEle mentById ("Table").inser tBefore (cloned_row, active_row)

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

var active_row = document.getEle mentById("Activ e_Row");
var cloned_row = active_row.clon eNode(true);
cloned_row.id = '';
active_row.pare ntNode.insertBe fore(cloned_row , active_row);
--
Rob

Dec 22 '06 #2
RobG wrote:
Janis Papanagnou wrote:
> active_row = document.getEle mentById ("Active_Row ")
cloned_row = active_row.clon eNode (true)
document.getEle mentById ("Table").appen dChild (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.getEle mentById("Activ e_Row");
var cloned_row = active_row.clon eNode(true);
cloned_row.id = '';
active_row.pare ntNode.appendCh ild(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.getEle mentById ("Active_Row ")
cloned_row = active_row.clon eNode (true)
document.getEle mentById ("Table").inser tBefore (cloned_row, active_row)

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

Try:

var active_row = document.getEle mentById("Activ e_Row");
var cloned_row = active_row.clon eNode(true);
cloned_row.id = '';
active_row.pare ntNode.insertBe fore(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
2835
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 DomDocument using getElementsByTagName() isn't found when I use insertBefore(). I blame the namespace :-) When I replace the getElementsByTagName( 'xsl:template' ), I get the "no template tag found message" in the else. What is the NS-safe way...
1
7307
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 id in the document with the onbeforeprint window call. The chosen image will then be inserted into the document as a child element of ‘fig'. The problem is the images decide on their own whether or not to show up to the party. Random images...
1
20705
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 init() {
1
1562
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 its index and then using the array sort with). The problem is with the following code that I took from MSDN's DHTML Dude's article: //TheBody is is a tbody with id "TheBody". It is at the end of the table...
5
3362
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
6138
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 Firefox, Safari and Konqueror, but the onchange event just doesn't fire on IE6. Firefox's javascript console shows no errors, and the IE script debugger shows nothing. onchange is not triggered.
13
11803
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 are involved in this problem, i post it here as well. If that is "illegal", please delete it, thanks.
2
7541
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 appendChild() and insertBefore(), then the appendChild() works for the first time and subsequently, insertBefore() works fine. Can someone tell how I can make insertBefore() work always? selectele = document.createElement("SELECT"); for (i = 0; i...
0
10571
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...
0
9143
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
7615
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
6851
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
5520
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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 we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.