It should be:
document.getElementById("placehere").appendChild(elem);
And put your div in front of your javascript, because if you do not, javascript will be executed before the div exists. Or wait for it to load. So your code is as follows:
<html> <body> <script type="text/javascript"> window.onload=function(){ var elem = document.createElement("img"); elem.setAttribute("src", "http://img.zohostatic.com/discussions/v1/images/defaultPhoto.png"); elem.setAttribute("height", "768"); elem.setAttribute("width", "1024"); elem.setAttribute("alt", "Flower"); document.getElementById("placehere").appendChild(elem); } </script> <div id="placehere"> </div> </body> </html>
To prove your point, see this one with onload and this one without onload . Launch the console and you will find an error message indicating that the div does not exist or cannot find the appendChild null method.
Some Guy Oct 18 2018-11-11T00: 00Z
source share