Javascript DOM HTML Column Object get

Introduction

The Column object represents an HTML <col> element.

We can access a <col> element via document.getElementById():

var x = document.getElementById("myCol");

Click the button to get the number of columns a COL element should span.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>//from   w w w.j  a  v  a  2 s  . com
</head>
<body>
<table>
  <colgroup>
    <col id="myCol" span="2" style="background-color:red">
    <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>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myCol").span;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

We can create a <col> element by using the document.createElement() method:

var x = document.createElement("COL");

Column Object Properties

Property Description
span Sets or returns the value of the span attribute of a column



PreviousNext

Related