I am just learning Javascript and need an answer to this question. Why doesn't this piece of javascript work in the HTML DOM using an internal script tag?
Here is my html doc. with an internal javascript extension:
<div id="targetarea">
<p>Hello World</p>
</div>
<div id="target-area">
<p id="tagline">Hello World!</p>
</div>
<script>
//Creating a a new element
// store the target area to a variable to keep things neat
var targetArea = document.getElementById("target-area");
// create our <p> element
var p = document.createElement("p");
// create a text node inside the <p>, note that we're
// using a variable "p" here
var snippet = document.createTextNode("this was a generated paragraph");
// insert our generated paragraph into the DOM
p.appendChild(snippet);
targetArea.appendChild(p);
</script>
This works fine internally but when use an external js file it does not. Can someone give me the right js snippet for this to work in an external file and explain why?