Javascript DOM HTML Table caption Property get

Introduction

Get the text inside a caption element:

Click the button to return the text inside the caption element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
  border: 1px solid black;
}
</style>//from   ww  w.java 2s  .com
</head>
<body>
<p id="demo"></p>

<table id="myTable">
  <caption>This is a table caption</caption>
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
  <tr>
    <td>cell 3</td>
    <td>cell 4</td>
  </tr>
</table><br>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = document.getElementById("myTable").caption.innerHTML;
}
</script>

</body>
</html>

The caption property returns the <caption> element of a table.

The <caption> element defines a caption for a table.

The caption property returns the <caption> element of the table, or null if it is not defined.




PreviousNext

Related