By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,231 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

insertBefore (oNewNode [, oChildNode]) problems

P: n/a
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(i nsultObj);

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

</body>
</html>

Thanks,
Derek Basch

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Jul 23 '05 #2

P: n/a
Derek Basch wrote:
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?

Jul 23 '05 #3

P: n/a
RobB wrote:
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>

Jul 23 '05 #4

P: n/a
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!
--
Rob
Jul 23 '05 #5

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.