insertBefore (oNewNode [, oChildNode]) problems

D

Derek Basch

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

<html>
<head>
</head>

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

<script language="JavaScript">

var insultObj = document.createTextNode("Could you *be* any dumber");
document.getElementById("heading1").insertBefore(insultObj);

var pObj = document.getElementById("heading1");
alert (pObj.childNodes[1].nodeName);
</script>

</body>
</html>

Thanks,
Derek Basch
 
D

Derek Basch

I should have also added that this is the error that I get:

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

Sorry about that.
 
R

RobB

Derek said:
I should have also added that this is the error that I get:

Error: uncaught exception: [Exception... "Not enough arguments
[nsIDOMText.insertBefore]" nsresult: "0x80570001
(NS_ERROR_XPC_NOT_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.insertBefore() 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?
 
D

Derek Basch

RobB said:
Pretty much answers your Q...Object.insertBefore() 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">Read between the lines</h1>
<script language="JavaScript">

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

heading = document.getElementById("heading");
heading.parentNode.insertBefore(insultObj, heading);
heading.parentNode.insertBefore(replyObj, null);

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

RobG

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.getElementById("heading");
heading.parentNode.insertBefore(insultObj, heading);
heading.parentNode.insertBefore(replyObj, null);

Cool. I have been using:

...insertBefore(replyObj,lastOne.nextSibling);

to "insertAfter" the last child. From now on I'll be using your
trick - thanks!
 
D

Derek Basch

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top