Share on Google+Share on Google+

JavaScript createCaption method

In the JavaScript we can add the table's caption dynamically by using the method createCaption().

JavaScript createCaption method


In the JavaScript we can add the table's caption dynamically by using the method createCaption(). The createCaption() method is used to create an empty caption element for a table. We can use the property innerHTML onto the created caption object to add the caption with some value.


   table_Object.createCaption() ;

Description of example:

In the following example code we have created a table which is not having any caption and we will add the caption to this table by using the method createCaption(). We have create a button element in the HTML page which will call the function createCaption() which we have defined in our script tags. To create caption we have used createCaption() method on the table's object and then to add some value to the caption we will use the innerHTML property of the caption object.

Here is the full example code of creating and adding the caption element to the table as described below:


  <script type="text/javascript">
   function createCaption()
     var cap=document.getElementById('table').createCaption();
<p align="center">&nbsp;</p>
 <div style="background: #cf2255; 
      <font color="#ffffcc" 
            Create Caption Example
    <table id="table" 
           cellpadding="3" >
<td align="center" bgcolor="#808080">
  <p align="center">Name</p>
<td align="center">
  <p align="center">Amit</p>
<td align="center" bgcolor="#808080">
  <p align="center">Qual</p>
<td align="center">
  <p align="center">MCA</p>
<p align="center">
<br />
<input type="button" onclick="createCaption();
        value="Create caption">


click on the button "Create caption" , it will create and add the table's caption into the table body.

Download Source Code



Posted on: January 29, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JavaScript createCaption method  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image