Create a <caption> element with some text for a table:
Click the button to create a caption element for the table.
<!DOCTYPE html> <html> <head> <style> table, td { border: 1px solid black; } </style>//from w w w.j ava 2s .co m </head> <body> <table id="myTable"> <tr> <td>cell 1</td> <td>cell 2</td> </tr> <tr> <td>cell 3</td> <td>cell 4</td> </tr> </table> <br> <button onclick="myFunction()">Test</button> <script> function myFunction() { var table = document.getElementById("myTable").createCaption(); table.innerHTML = "<b>My table caption</b>"; } </script> </body> </html>
The createCaption()
method creates an empty <caption> element and adds it to the table.
If a <caption> element exists on the table, the createCaption()
method returns the existing one, and does not create a new one.
The createCaption()
method returns the newly created or an existing <caption> element.