1. Computing & Technology

JavaScript By Example

DOM Table: 2. Table Caption

From , former About.com Guide

When we are processing a table node there is one property and two methods that allow us to directly access the caption node within the table without our needing to use the standard DOM node processing to do so.

The caption property allows us to directly access the text content of the caption (if it exists). The createCaption() method allows us to add a caption to the table and the deleteCaption() method allows us to remove the caption node from within the table. A table can have only one caption and so these allow the caption node within the table to be accessed directly without needing to use getElementsByTagName('caption')[0] to hunt for it.

If the following JavaScript is attached to the HTML from our first DOM table example it will replace the text "example table" in the caption (while leaving the summary attribute alone).


var mytable;
mytable = document.getElementById('mytab');
mytable.caption = 'replacement caption for table';

©2012 About.com. All rights reserved.

A part of The New York Times Company.