Javascript Reference - HTML DOM Figcaption Object








The Figcaption object represents an HTML <figcaption> element.

Standard Properties and Events

The Figcaption object supports the standard properties and events.

Example

We can access a <figcaption> element by using getElementById().


<!DOCTYPE html>
<html>
<body>
<figure>
  <img src="http://java2s.com/style/demo/border.png" alt="test" width="100" height="100">
  <figcaption id="myFigCap">Fig.1 - An image.</figcaption>
</figure><!--  w w w  .  j  a va  2 s . c  o m-->
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    var x = document.getElementById("myFigCap");
    x.style.color = "red";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

We can create a <figcaption> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button><br><br>
<!--   w w w  . ja va 2s. co m-->
<figure id="myFigure">
  <img src="http://java2s.com/style/demo/border.png" alt="test" width="304" height="228">
</figure>

<script>
function myFunction() {
    var x = document.createElement("FIGCAPTION");
    var t = document.createTextNode("Fig.1 - A caption.");
    x.appendChild(t);
 
    var y = document.getElementById("myFigure");
    y.appendChild(x);
}
</script>

</body>
</html>

The code above is rendered as follows: