Table tBodies Collection - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Table

Description

The tBodies collection returns all <tbody> elements in a table sorted as they appear in the HTML code.

Syntax

Properties

Property Description
length number of <tbody> elements in the collection.

Methods

MethodDescription
[index] <tbody> element with the specified index (starts at 0).
item(index) <tbody> element with the specified index (starts at 0).
namedItem(id) <tbody> element with the specified id.

Return Value:

An HTMLCollection Object, representing all <tbody> elements in the <table> element.

The following code shows how to Find out how many <tbody> elements there are in a table:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>/*from   w  w w . j  a va  2 s.com*/
</head>
<body>

<table id="myTable">
<tbody>
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
</tbody>
<tbody>
  <tr>
    <td>cell 3</td>
    <td>cell 4</td>
  </tr>
</tbody>
</table>
<br>

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

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

<script>
function myFunction() {
    var x = document.getElementById("myTable").tBodies.length;
    document.getElementById("demo").innerHTML = "Found " + x + " tbody elements in the table.";
}
</script>

</body>
</html>

Related Tutorials