Set the background color of the first two columns to red:
Click the button to set the background color of the first two columns to red.
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; } </style>//w w w . j a v a 2 s .c om </head> <body> <table> <colgroup> <col id="myCol"> <col style="background-color:yellow"> </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> <p id="demo"></p> <button onclick="myFunction()">Test</button> <script> function myFunction() { document.getElementById("myCol").span = "2"; document.getElementById("myCol").style.backgroundColor = "red"; } </script> </body> </html>
The span
property sets or gets the span attribute of a column.
The span attribute defines the number of columns a <col> element should span.
The span
property accepts and returns a number value.