Javascript Reference - HTML DOM TableHeader rowSpan Property








The rowSpan property sets or gets the rowspan attribute.

The rowspan attribute specifies the number of rows a table cell should span.

Browser Support

rowSpan Yes Yes Yes Yes Yes

Syntax

Return the rowSpan property.

var v = tableheaderObject.rowSpan

Set the rowSpan property.

tableheaderObject.rowSpan=number

Property Values

Value Description
number Specifies the number of rows a cell should span




Return Value

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

Example

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


<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {<!-- w  ww.java  2 s. c o m-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
  <tr>
    <th id="myTh" rowspan="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>

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

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

</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {<!--  w w  w .j  ava 2  s. com-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
  <tr>
    <th id="myTh" rowspan="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").rowSpan = "1";
}
</script>

</body>
</html>

The code above is rendered as follows: