Javascript DOM HTML Figcaption Object create

Introduction

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

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

Click the button to create a FIGCAPTION element with some text.

View in separate window

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

<figure id="myFigure">
  <img src="image1.png" alt="The Circle" width="100" height="100">
</figure>//from ww w .  ja v a  2  s.c  om

<script>
function myFunction() {
  var x = document.createElement("FIGCAPTION");
  var t = document.createTextNode("Fig.1 - A picture of a circle.");
  x.appendChild(t);

  var y = document.getElementById("myFigure");
  y.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related