Javascript DOM HTML Figure Object create

Introduction

We can create a <figure> element via the document.createElement() method:

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

Click the button to create a FIGURE element containing an IMG element.

View in separate window

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

<script>
function myFunction() {/*from w w w . ja  va  2s. c o m*/
  var x = document.createElement("FIGURE");
  x.setAttribute("id", "myFigure");
  document.body.appendChild(x);

  var y = document.createElement("IMG");
  y.setAttribute("src", "image1.png");
  y.setAttribute("width", "100");
  y.setAttribute("height", "98");
  y.setAttribute("alt", "The Circle");

  x.appendChild(y);
}
</script>

</body>
</html>



PreviousNext

Related