Here's how to implicitly get the last cell. Note that you can reference a specific object in an array of rows or cells using square bracket notation, and due to the indexing of arrays starting at 0, you must subtract 1 from the length to use it as the actual last index.
This example shows the cases you can do:
<!DOCTYPE html> <html> <head> <title>Table test</title> <script language="javascript" type="text/javascript"> function init() { var table = document.getElementById("table1"); var lastRowIndex = table.rows.length-1; var lastCellIndex = table.rows[lastRowIndex].cells.length-1; alert( table.rows[lastRowIndex].cells[lastCellIndex].innerHTML ); </script> </head> <body> <table id="table1"> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>4</td><td>5</td><td>6</td></tr> <tr><td>7</td><td>8</td><td>9</td></tr> </table> </body> </html>
Suppose you had an arbitrary table with 8 columns and 4 rows, as in the following example, and demanded that the cell in column 5 and row 3 receive this cell, which you would like to get a link to the table (for example, via getElementById ), and then select for the right row and column through rows and cells to subtract 1 due to indexing arrays starting from 0. See this example:
<!DOCTYPE html> <html> <head> <title>Table test</title> <script language="javascript" type="text/javascript"> function init() { var table = document.getElementById("table1"); var column5Row3 = table.rows[2].cells[4]; </script> </head> <body> <table id="table1"> <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr> <tr><td>2</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td></tr> <tr><td>3</td><td>x</td><td>x</td><td>x</td><td>Y</td><td>x</td><td>x</td><td>x</td></tr> <tr><td>4</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td></tr> </table> </body> </html>
source share