Home > DeveloperSection > Forums > getElementById and onLoad using javascript
Goti Bandu

Total Post:119

Posted on    August-19-2013 12:45 AM

 JavaScript JavaScript 

 1 Reply(s)
 1969  View(s)
Rate this:
Hi Mindstick!

HTML file ...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="src/myJS.js"></script>


<body onload="myJS.myFunction();">

<p id="p01"></p>



External Javascript file (called myJS.js for convenience) ...

myJS = {

    myFunction: function()


        //This works

        document.write("Hello world. ");

        //This does not work

        document.getElementById("p01").appendChild(document.createTextNode("Hello world, again"));



My best guess is that the node p01 has not been created when myJS is executed, but I thought that onload would do the right thing with it.


shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Posted on    August-19-2013 8:33 AM


Because p01 is not yet added to the DOM. 

Try like this: 


<p id="p01"></p>

<script type="text/javascript">

window.onload = myJS.myFunction;



Don't want to miss updates? Please click the below button!

Follow MindStick