James Jefferies and ShedCode Ltd

The world of James and ShedCode

Adding a Link in Javascript

thank you Jamie!

(but then I found this too with prototype)

1
$('container').update('<a href="#" id="myclickid">text for link</a>');

which will update a container called div and pop in the following stuff

1
<div id="container"></div>

should become

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div id="container"><a href="#" id="myclickid">text for link</a></div>

function addLink() {

    var closeP = document.createElement("p");
    closeP.appendChild(document.createTextNode("Alternatively, ");

    var closeLink = document.createElement("a");
    closeLink.href = "#";
    closeLink.onclick = function() { window.close(); };
    closeLink.appendChild(document.createTextNode("Close window"));
    closeP.appendChild(closeLink);

    var parentEl = document.getElementById("container");
    // this should be the ID of the element this link should be appended to
    parentEl.appendChild(closeP);
    }

    if (window.addEventListener) { window.addEventListener("load", addLink, false); }
    else if (window.attachEvent) { window.attachEvent("onload", addLink); }