Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how to implicitly get the last cell. Note that you can reference a particular object in the rows or cells array using the square bracket notation, and that due to arrays indexing starting at 0 you must subtract 1 from the length to use it as a valid last index.</p> <p>This example show cases some of the things you can do:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Table test&lt;/title&gt; &lt;script language="javascript" type="text/javascript"&gt; 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 ); // alerts the cell's containing HTML, or 9 var lastCell = table.rows[lastRowIndex].cells[lastCellIndex]; // contains a reference to the last cell } window.onload = init; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;table id="table1"&gt; &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Suppose you had an arbitrary table with 8 columns and 4 rows as in the following example, and wanted cell in column 5 and row 3 to get that cell you'd want to grab a reference to the table (such as through <code>getElementById</code>) and then select for the right row and column through <code>rows</code> and <code>cells</code> making sure to subtract 1 due to arrays indexing starting at 0. See this example:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Table test&lt;/title&gt; &lt;script language="javascript" type="text/javascript"&gt; function init() { var table = document.getElementById("table1"); var column5Row3 = table.rows[2].cells[4]; // contains a reference to the cell that is in the 3rd row, and 5th column alert( column5Row3.innerHTML ); // alerts that cell's innerHTML (or Y) } window.onload = init; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;table id="table1"&gt; &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;Y&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;x&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload