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

Need help with insertBefore

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a


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

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

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

Replies have been disabled for this discussion.