473,396 Members | 2,106 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.

Need help with insertBefore

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")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;
var base = document.getElementsByTagName("PRE")[0];
var link = document.createElement("SPAN");
var text = document.createTextNode("Copy All");
link.appendChild(text);
for (var i=0;i<where.length;i++) {
if (where[i].nodeName == 'PRE'))
document.body.insertBefore(link,where[i]);
}
}

Thank you so much, Dante.
Jul 20 '05 #1
3 4685


Dante wrote:
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")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;


The problem might be all those childNodes references, don't make
assumptions about childNodes being (particular) element nodes, there are
text nodes in the DOM of Mozilla and Opera for instance, and there are
<tbody> element nodes included by the SGML/HTML parser for instance.
If you want to insert into certain elements then use
element.getElementsByTagName
or loop through childNodes but make sure that you check for nodeType and
tagName.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
pi******@reno.com (Dante) writes:
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.
If you tell us what goes wrong, we won't have to guess.
The three questions one should answer before asking for help in debugging:

- What do you do (the full code, which browser, how to reproduce the
bug - i.e., which buttons to press in which order)
- What you expect to happen (in detail, e.g., before each PRE element,
there should appear the text "Copy All").
- What really happens (in detail)

We should be able to try to reproduce the problem, recognize the problem
when it happens, and know what the desired behavior should be, in order
to find and correct the bug.
Can anyone help me?
Maybe :)
function makeIt() {
var where = document.getElementsByTagName("TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;
The fourth table's first child's first child's first child's children.

That would be the children of the first cell of the first row of the
first rowgroup (which is probably a tbody), correct?

Why not just give that td an id? Or use
document.getElementsByTagName("table").rows[0].cells[0].childNodes;
?
var base = document.getElementsByTagName("PRE")[0];
First PRE in the document. Not used?
var link = document.createElement("SPAN");
var text = document.createTextNode("Copy All");
link.appendChild(text);
So "link" is "<span>Copy All</span>"
for (var i=0;i<where.length;i++) {
if (where[i].nodeName == 'PRE'))
document.body.insertBefore(link,where[i]);


If you find more than one PRE, insert the link before each - in
effect, insert it before the last one, since one node can only appear
once in a document. Inserting it again will move it from its previous
position.

How about:

---
function makeIt() {
var where = document.getElementsByTagName("table")[4].firstChild.firstChild.firstChild;
var pres = where.getElementsByTagName("pre");
for (var i=0;i<pres.length;i++) {
var pre = pres[i];
var link = document.createElement("span");
link.appendChild(document.createTextNode("Copy All"));
// or make it a link or something
pre.parentNode.insertBefore(link,pre);
}
}
---
(not tested, as I don't have a suitable test page).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Sorry I was too vague. I don't know what caused the "Invalid Argument"
with insertBefore. I hate those errors that don't tell me what's
wrong; they just say something's wrong.

Thanks.
Jul 20 '05 #4

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

Similar topics

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...
5
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
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...
4
by: VA | last post by:
I have the following code (modified) from RobG on this newsgroup... function moveColumn(table_id,col_id, dir) { var table=document.getElementById(table_id); var idx=GetCellIndex(col_id,table);...
2
by: Rick | last post by:
Hello, I've been working at this script for a while and I've run into a problem. Basically what I'm trying to do is to create a row of three textboxes with three different names and headers...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.