473,587 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insertBefore (oNewNode [, oChildNode]) problems

Can anyone tell me why I cant insertBefore() objects that are selected
using getElementById( )?

<html>
<head>
</head>

<body>
<p id="heading1"> </p>

<script language="JavaS cript">

var insultObj = document.create TextNode("Could you *be* any dumber");
document.getEle mentById("headi ng1").insertBef ore(insultObj);

var pObj = document.getEle mentById("headi ng1");
alert (pObj.childNode s[1].nodeName);
</script>

</body>
</html>

Thanks,
Derek Basch

Jul 23 '05 #1
5 3351
I should have also added that this is the error that I get:

Error: uncaught exception: [Exception... "Not enough arguments
[nsIDOMText.inse rtBefore]" nsresult: "0x80570001
(NS_ERROR_XPC_N OT_ENOUGH_ARGS) " location: "JS frame ::
http://172.20.0.70/evaluation_framework/test.htm :: <TOP_LEVEL> :: line
12" data: no]

Sorry about that.

Jul 23 '05 #2
Derek Basch wrote:
I should have also added that this is the error that I get:

Error: uncaught exception: [Exception... "Not enough arguments
[nsIDOMText.inse rtBefore]" nsresult: "0x80570001
(NS_ERROR_XPC_N OT_ENOUGH_ARGS) " location: "JS frame ::
http://172.20.0.70/evaluation_framework/test.htm :: <TOP_LEVEL> :: line 12" data: no]

Sorry about that.


Pretty much answers your Q...Object.inse rtBefore() takes two arguments,
the child node being inserted, and the (existing) sibling node you want
to follow it. It's invoked, naturally, on the parent node of both
children.

More to the point: what exactly are you trying to do?

Jul 23 '05 #3
RobB wrote:
Pretty much answers your Q...Object.inse rtBefore() takes two arguments, the child node being inserted, and the (existing) sibling node you want to follow it. It's invoked, naturally, on the parent node of both
children.
Ahhhh! It is invoked on the parent node. That is what I wasn't groking.
I was thinking the method was invoked on the node that you wanted to
"insert" the newChild "before".

Correctly, it is invoked on the parent and you pass the newChild and
and refChild parameters. "Reference CHILD" being the key point here ;).

-------------------------From W3 DOM2:


insertBefore
Inserts the node newChild before the existing child node refChild.
If refChild is null, insert newChild at the end of the list of
children.
.....
Parameters
newChild of type Node
The node to insert.
refChild of type Node
The reference node, i.e., the node before which the new node
must be inserted.
-------------------------

According to the W3 DOM the refChild parameter can be null if I don't
care about where my newChild is inserted. So I made refChild a null
keyword and it works great now. I guess the absence of a null keyword
is not equivalent to the presence of a null keyword. Thanks for the
help!

<html>
<head>
</head>
<body>
<h1 id="heading">Re ad between the lines</h1>
<script language="JavaS cript">

var insultObj = document.create TextNode("Could you *be* any dumber");
var replyObj = document.create TextNode("Yes, just give me some time");

heading = document.getEle mentById("headi ng");
heading.parentN ode.insertBefor e(insultObj, heading);
heading.parentN ode.insertBefor e(replyObj, null);

</script>
</body>
</html>

Jul 23 '05 #4
Derek Basch wrote:
[...]
insertBefore
Inserts the node newChild before the existing child node refChild.
If refChild is null, insert newChild at the end of the list of
children. [...] heading = document.getEle mentById("headi ng");
heading.parentN ode.insertBefor e(insultObj, heading);
heading.parentN ode.insertBefor e(replyObj, null);


Cool. I have been using:

...insertBefore (replyObj,lastO ne.nextSibling) ;

to "insertAfte r" the last child. From now on I'll be using your
trick - thanks!
--
Rob
Jul 23 '05 #5
Glad I could pay you back for answering my DOM selectors question
yesterday :). I hope that this discussion can help someone in the
future as well.

Jul 23 '05 #6

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

Similar topics

3
2829
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...
3
4704
by: Dante | last post by:
Tonight I started writing a script that places a "Copy All" link before every PRE tag. The link would copy the code in the pre tag. I can't get this script to work. Can anyone help me? Here is my script: function makeIt() { var where = document.getElementsByTagName("TABLE").childNodes.childNodes.childNodes.childNodes; var base =...
1
1547
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...
2
2001
by: e michael brandt | last post by:
I am frustrated. It appears that one can not use insertBefore to insert an A node before a DL node. Is that really true? I *am* able to insert before a <p id="hh"> tag, but not before a <dl id="hh"> tag. i am able to do a body.appendChild with no trouble. Finally, winIE6 pops an error: 'Invalid argument' at the last snip line below...
2
8972
by: Janis Papanagnou | last post by:
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...
2
7517
adarshtp
by: adarshtp | last post by:
Hi all, i have a Javascript function as shown: function move_row(tr_id,position){ var parentTable = document.getElementById("TABLE"); var table_length = parentTable.rows.length; if(parentTable.nodeName != 'TBODY') parentTable = parentTable.getElementsByTagName('TBODY'); if(parentTable.nodeName != 'TBODY')
2
7530
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...
3
8392
by: masterofzen | last post by:
I've been playing around with this for a while. I bet the answer is pretty obvious, but I'm just not seeing it. I'm also reasonably certain I've run into this problem before. I'm trying to dynamically insert a few nodes into a webpage using insertBefore. Here's the HTML in question. (Yes, I know the HTML is terrible, but I didn't write it and...
0
7920
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...
0
8215
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. ...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
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...
1
5718
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...
0
5394
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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

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.