The Caption object represents an HTML <caption> element.
We can access a <caption> element by using getElementById()
:
var x = document.getElementById("myCaption");
Click the button to set the color of the caption to red.
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; } </style>/*from w ww . ja v a 2 s .c o m*/ </head> <body> <table> <caption id="myCaption">Monthly Salary</caption> <tr> <th>Item</th> <th>Savings</th> </tr> <tr> <td>CSS</td> <td>$100</td> </tr> <tr> <td>HTML</td> <td>$50</td> </tr> </table> <button onclick="myFunction()">Test</button> <script> function myFunction() { var x = document.getElementById("myCaption"); x.style.color = "red"; } </script> </body> </html>