Adding images to an HTML document with JavaScript

You need to use document.getElementById() in line 3.

If you try this right now in the console:

var img = document.createElement("img");
img.src = "http://www.google.com/intl/en_com/images/logo_plain.png";
var src = document.getElementById("header");
src.appendChild(img);
<div id="header"></div>

… you’d get this:

enter image description here

Leave a Comment