---
title: "getElementById and onLoad using javascript"  
description: "getElementById and onLoad using javascript"  
author: "Anonymous User"  
published: 2013-08-19  
updated: 2013-08-19  
canonical: https://www.mindstick.com/forum/1398/getelementbyid-and-onload-using-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# getElementById and onLoad using javascript

Hi [Mindstick](https://yourviews.mindstick.com/view/84668/how-mindstick-is-used-for-marketing-purposes)!\

[HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) ...

```
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"
content="text/html; charset=UTF-8"><script type="text/javascript"
src="src/myJS.js"></script></head><body onload="myJS.myFunction();"><p id="p01"></p></body></html> 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](https://www.mindstick.com/forum/161418/what-is-node-js-and-how-does-it-work) p01 has not been created when myJS is executed, but I thought that onload would do the [right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not) thing with it.

thanks\

## Replies

### Reply by shreesh chandra shukla

Hi!

Because p01 is not yet added to the DOM.

## Try like this:

<body>

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

<script type="text/javascript">

window.onload = myJS.myFunction;

</script>

</body>


---

Original Source: https://www.mindstick.com/forum/1398/getelementbyid-and-onload-using-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
