The ColumnGroup
object represents an HTML <colgroup> element.
We can access a <colgroup> element via document.getElementById()
:
var x = document.getElementById("myColgroup");
Click the button to get the number of columns a COLGROUP
element should span.
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; } </style>//from w ww .ja v a2 s .c om </head> <body> <table> <colgroup id="myColgroup" span="2" style="background:red"></colgroup> <tr><th>ISBN</th> <th>Title</th> <th>Price</th></tr> <tr><td>101</td> <td>HTML</td> <td>$5</td></tr> <tr><td>102</td> <td>CSS</td> <td>$4</td></tr> </table> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myColgroup").span; document.getElementById("demo").innerHTML = x; } </script> </body> </html>