add image in javascript code example

Example 1: javascrpit add image element

function add_img() { 
	var img = document.createElement('img'); 
    img.src = 'https://media.geeksforgeeks.org/wp-content/uploads/20190529122828/bs21.png'; 
	document.getElementById('body').appendChild(img);
}

Example 2: javascript appendchild image node

let img = document.createElement("img");
    img.src = source.getAttribute('data-image');
    document.getElementByID("target").appendChild(img);

Example 3: how to set diferent images in html through js

<div id="x"></div>

<script>
var img = document.createElement("img");
 
img.src = "image.png";
var src = document.getElementById("x");
 
src.appendChild(img);
</script>

Tags:

Html Example