0

I'm trying to append a link to my html-document. It works in JSFiddle but not with a html-document and js-file. Can anyone please se what I'm doing wrong.

fiddle: http://jsfiddle.net/2LcRF/

HTML-code: Javascript Hidden textarea

<body>

<div id="container">
    <div id="content">
    <p>hello</p>


    </div>

</div>
</body>
</html>

JavaScript-code;

var test= document.getElementById("content");
var a = document.createElement("a");
a.href = "#"
var text = document.createTextNode("länk");
a.appendChild(text);
test.appendChild(a);
Delal
  • 131
  • 2
  • 2
  • 10

1 Answers1

0

What is the point of creating a text node? Just set the innerHTML.

var test= document.getElementById("content");
var a = document.createElement("a");
a.href = "#"
a.innerHTML = "länk";
test.appendChild(a);

Also, where is the script tag? If you're not using a window.onload listener, the script tag should be added as the last item in body

posit labs
  • 8,951
  • 4
  • 36
  • 66