The colSpan
property sets or gets the colspan attribute.
The colspan
attribute specifies the number of columns a table cell should span.
colSpan |
Yes | Yes | Yes | Yes | Yes |
Return the colSpan property.
var v = tabledataObject.colSpan
Set the colSpan property.
tabledataObject.colSpan=number
Value | Description |
---|---|
number | Specifies the number of columns a cell should span |
A Number type value representing the number of columns a table cell should span.
The following code shows how to change the number of columns a table cell should span.
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {<!--from w w w . ja v a2 s .c o m-->
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td id="myTd" colspan="2">A</td>
<td>$1</td>
</tr>
<tr>
<td>February</td>
<td>$0</td>
</tr>
</table>
<br>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myTd").colSpan = "1";
}
</script>
</body>
</html>
The code above is rendered as follows:
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 ww.j a v a 2 s. co m-->
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td id="myTd" colspan="2">A</td>
<td>$1</td>
</tr>
<tr>
<td>B</td>
<td>$10</td>
</tr>
</table>
<br>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction(x) {
var x = document.getElementById("myTd").colSpan;
document.getElementById("demo").innerHTML = "The value of colspan is: " + x;
}
</script>
</body>
</html>
The code above is rendered as follows: