1

This is the start of my JavaScript Code

   var para = document.createElement("p");
    var node = document.createTextNode("This is new.");
    para.appendChild(node);
    var element = document.getElementById("test");
    element.appendChild(para);

Below is the body

          <div id="test"> </div>

P.S. I am fairly new to JavaScript and thank you for your time.

Jim
  • 13
  • 2

2 Answers2

0

I guess you want to execute your function on load, otherwise you can't get your element with id test form the DOM, because it doesn't exist before the page is loaded:

window.onload=function(){
   var para = document.createElement("p");
   var node = document.createTextNode("This is new.");
   para.appendChild(node);
   var element = document.getElementById("test");
   element.appendChild(para);
}
nicael
  • 16,503
  • 12
  • 50
  • 80
  • Thank you. I used functions before, and I did not realize it had to be a function. Now I know, and I am very thankful for you figuring out my problem. – Jim Mar 27 '15 at 19:12
0

This seems to work fine using jsfiddle, there may be something else not posted that is interfering with the example you gave.

http://jsfiddle.net/dkLyfzze/

var para = document.createElement("p");
    var node = document.createTextNode("This is new.");
    para.appendChild(node);
    var element = document.getElementById("test");
    element.appendChild(para);
<div id="test"> </div>
Allen Tellez
  • 1,048
  • 11
  • 14