473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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="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
5 3341
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
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
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
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
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
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...
3
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...
1
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...
2
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...
2
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...
2
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;...
2
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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
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,...

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.