Javascript DOM HTML Image Object create

Create an Image Object

We can create an <img> element via the document.createElement() method:

Click the button to create an IMG element.

var x = document.createElement("IMG");

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from   www.j a va2s .c o m
  var x = document.createElement("IMG");
  x.setAttribute("src", "image1.png");
  x.setAttribute("width", "100");
  x.setAttribute("height", "100");
  x.setAttribute("alt", "The Circle");
  document.body.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related