Create a Caption Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Caption

Introduction

You can create a <caption> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    margin-top: 20px;
}
</style>/*  ww  w.  j  ava2 s .  c o m*/
</head>
<body>

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

<table id="myTable">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

<script>
function myFunction() {
    var x = document.createElement("CAPTION");
    var t = document.createTextNode("this is the caption");
    x.appendChild(t);

    var table = document.getElementById("myTable")
    table.insertBefore(x, table.childNodes[0]);
}
</script>

</body>
</html>

Related Tutorials