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.'