/

HTML / CSS / JavaScript Tutorial

JavaScript table object property: caption

[this page | pdf | back links]

The caption property of the JavaScript DOM object corresponding to the HTML <table> element returns the <caption> element of the table.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,td,tr,th,caption {border: thin solid black; border-collapse: collapse;}
</style>
</head>
<body>
Example table:
<table id="element">
  <caption>A table</caption>
  <thead>
    <tr><th>col1</th><th>col2</th></tr>
    <tr><td>Entry 1</td><td>Entry 2</td></tr>
  </thead>
  <tbody>
    <tr><td>A1</td><td>B1</td></tr>
  </tbody>
  <tbody>
    <tr><td>A2</td><td>B2</td></tr>
  </tbody>
  <tfoot>
    <tr><td>a</td><td>b</td></tr>
  </tfoot>
</table><br>

Details of above table extracted using JavaScript:
<table>
  <tr><td>Table caption</td><td id="x1"></td></tr>
  <tr><td>Number of rows (overall)</td><td id="x2"></td></tr>
  <tr><td>Number of tbody elements</td><td id="x3"></td></tr>
  <tr><td>Number of rows in thead element</td><td id="x4"></td></tr>
  <tr><td>Number of rows in first tbody</td><td id="x5"></td></tr>
  <tr><td>Number of rows in second tbody</td><td id="x6"></td></tr>
  <tr><td>Number of rows in tfoot</td><td id="x7"></td></tr>
  <tr><td>Text in first cell of second overall row</td><td id="x8"></td></tr>
  <tr><td>Text in second cell of first row of second tbody</td><td id="x9"></td></tr>
</table>

<script>
var x = document.getElementById("element");
document.getElementById("x1").innerHTML = x.caption.innerHTML;
document.getElementById("x2").innerHTML = x.rows.length;
document.getElementById("x3").innerHTML = x.tBodies.length;
document.getElementById("x4").innerHTML = x.tHead.rows.length;
document.getElementById("x5").innerHTML = x.tBodies[0].rows.length;
document.getElementById("x6").innerHTML = x.tBodies[1].rows.length;
document.getElementById("x7").innerHTML = x.tFoot.rows.length;
document.getElementById("x8").innerHTML = x.rows[1].cells[0].innerHTML;
document.getElementById("x9").innerHTML = x.tBodies[1].rows[0].cells[1].innerHTML;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile