
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM Caption Object
The HTML DOM Caption object is associated with the HTML <caption> element. The <caption> element is used for setting caption (title) of the table and should be the first child of the table. You can access caption element using the caption object.
Properties
Note: The below property are not supported in the HTML5.
Following is the HTML DOM Caption Object property −
Property | Description |
---|---|
Align | To set or return the caption alignment. |
Syntax
Following is the syntax for −
Creating a caption object −
var x = document.createElement("CAPTION");
Example
Let us see an example for the HTML DOM Caption object −
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px double black; margin-top: 14px; } </style> </head> <body> <p>Click the button below to create the caption for the table.</p> <button onclick="createCaption()">CREATE</button> <table id="SampleTable"> <tr> <td colspan="2" rowpan="2">TABLE</td> </tr> <tr> <td>Value 1</td> <td>Value 2</td> </tr> </table> <script> function createCaption() { var x = document.createElement("CAPTION"); var t = document.createTextNode("TABLE CAPTION"); x.appendChild(t); var table = document.getElementById("SampleTable") table.insertBefore(x, table.childNodes[0]); } </script> </body> </html>
Output
This will produce the following output −
On clicking the CREATE button −
In the above example −
We have first created a button CREATE to execute createCaption() method on click −
<button onclick="createCaption()">CREATE</button>
The createCaption() method creates a caption element using the createElement() method of the document object and assigned it to variable x. It then created a text node with “TABLE CAPTION” text. We then appended the text node to the element.
Finally, we get the <table> element using the id “SampleTable” and using the insertBefore() method inserted the caption along with the text node as the first child of the table. The <caption> can only be the first child of a table −
function createCaption() { var x = document.createElement("CAPTION"); var t = document.createTextNode("TABLE CAPTION"); x.appendChild(t); var table = document.getElementById("SampleTable") table.insertBefore(x, table.childNodes[0]); }
- Related Articles
- HTML DOM Table caption Property
- HTML DOM Object Object
- HTML DOM HTML Object
- HTML caption Tag
- HTML DOM Ins Object
- HTML DOM HR Object
- HTML DOM Variable Object
- HTML DOM aside Object
- HTML DOM Anchor Object
- HTML DOM TableData Object
- HTML DOM TableHeader Object
- HTML DOM TableRow Object
- HTML DOM Textarea Object
- HTML DOM Meter Object
- HTML DOM MouseEvent Object
