Javascript Reference - HTML DOM TableHeader colSpan Property








The colSpan property sets or gets the value of the colspan attribute.

The colspan attribute specifies the number of columns a table cell should span.

Browser Support

colSpan Yes Yes Yes Yes Yes

Syntax

Return the colSpan property.

var v = tableheaderObject.colSpan

Set the colSpan property.

tableheaderObject.colSpan=number




Property Values

Value Description
number Set the number of columns a cell should span

Return Value

A Number type value representing the number of columns a table cell should span.

Example

The following code shows how to get the number of columns a specific table cell should span.


<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {<!-- w  w w .j av  a  2  s . c om-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
  <tr>
    <th id="myTh" colspan="2">Item</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>A</td>
    <td>$1</td>
  </tr>
  <tr>
    <td>B</td>
    <td>$0</td>
  </tr>
</table>
<br> 

<button onclick="myFunction()">test</button>

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

<script>
function myFunction(x) {
    var x = document.getElementById("myTh").colSpan;
    document.getElementById("demo").innerHTML = "The value of colspan is: " + x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 3

The following code shows how to change the number of columns a cell should span.


<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {<!-- w w w  .  j  a  v a 2s  .  co m-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
  <tr>
    <th id="myTh" colspan="2">Item</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>A</td>
    <td>$1</td>
  </tr>
  <tr>
    <td>B</td>
    <td>$1</td>
  </tr>
</table>
<br> 

<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myTh").colSpan = "1";
}
</script>

</body>
</html>

The code above is rendered as follows: