Table createCaption()Method
Table Object
Definition and Usage
The createCaption() method is used to get or create a <caption> element in a table.
Note:If the caption element already exists in the table, the createCaption() method returns the existing caption rather than creating a new <caption> element.
Tip:To remove the caption element from a table, use thedeleteCaption()method.
Syntax
tableObject.createCaption()
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the createCaption() method
Example
Example
The following example creates a caption for a table:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
<script>
function displayResult(){
var x=document.getElementById("myTable").createCaption();
x.innerHTML="<b>My table title</b>";
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">Create a table caption</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
<script>
function displayResult(){
var x=document.getElementById("myTable").createCaption();
x.innerHTML="<b>My table title</b>";
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">Create a table caption</button>
</body>
</html>
Try it yourself »
Table Object Other Extensions