Create an Image Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Introduction

You can create an <img> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create an IMG element</button>

<script>
function myFunction() {/*from   w  ww  .j av a 2  s.c o m*/
    var x = document.createElement("IMG");
    x.setAttribute("src", "http://java2s.com/resources/a.png");
    x.setAttribute("width", "304");
    x.setAttribute("height", "200");
    x.setAttribute("alt", "alt message");
    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials