/

HTML / CSS / JavaScript Tutorial

JavaScript table object method: createCaption()

[this page | pdf | back links]

The createCaption() method of the JavaScript DOM object corresponding to the HTML <table> element creates an empty <caption> element and adds it to the table. If a <caption> element already exists in the table then it returns the existing one, without creating a new one.

 

It has the following syntax with no parameters:

 

object.createCaption()

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,td,tr,caption {border: thin solid black; border-collapse: collapse;}
</style>
</head>
<body>
<span id="element"></span>

<script>
var x = document.createElement("TABLE");
x.createCaption().innerHTML = "A table";

var y0 = x.insertRow(0);
var y00 = y0.insertCell(0);
y00.innerHTML = "col1";
y00.style.color = "blue";
var y01 = y0.insertCell(1);
y01.innerHTML = "col2";
y01.style.color = "blue";

document.getElementById("element").appendChild(x);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodTableCreateCaption() {
  var z = document.createElement("TABLE"); return !!z.caption;
}


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


Desktop view | Switch to Mobile