Javascript examples for DOM HTML Element:Table
The cells collection returns all <td> or <th> elements in a table sorted as they appear in the source code.
Property | Description |
---|---|
length | number of <td> and/or <th> elements in the collection. |
Method | Description |
---|---|
[index] | <td> and/or <th> element with the specified index (starts at 0). |
item(index) | <td> and/or <th> element with the specified index (starts at 0). |
namedItem(id) | <td> and/or <th> element with the specified id. |
An HTMLCollection Object, representing all <td> and/or <th> elements in the <table> element.
The following code shows how to Find out how many cells there are in the first row in a table:
<!DOCTYPE html> <html> <head> <style> table, td { border: 1px solid black; } </style>/*from ww w . j av a 2 s .com*/ </head> <body> <table id="myTable"> <tr> <td>cell 1</td> <td>cell 2</td> </tr> <tr> <td>cell 3</td> <td>cell 4</td> </tr> </table> <br> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myTable").rows[0].cells.length; document.getElementById("demo").innerHTML = "Found " + x + " cells in the first tr element."; } </script> </body> </html>