Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last row of table attribute

Tags:

javascript

dom

var table = document.getElementById(table1);
var lastRow = table.rows.length;

var country = lastRow.getElementById('Country');
country.setAttribute('class', "Country"+lastRow);   

Im pretty new and I was wondering if this is possible?

Instead of

var Country= document.getElementById('Country');

As I have other id=Country because I am adding new rows..

EDIT

I know ID are unique. But I am cloning the last row so of course the IDs will all be the same.

like image 376
LandY Avatar asked Mar 07 '12 21:03

LandY


People also ask

How to get the last row of a table in MySQL?

Approach 1: First, select the table by its ID. Use find () method to find the all table rows of the table. Use last () method to get the last row of table. The background color of the last element has been changed to see the effect.

How do I find the last element of a table?

Approach 2: Use $ (‘table tr:last’) jQuery Selector to find the last element of the table. The ‘table’ in query looks for the table element then ‘tr’ is looking for all the rows in the table element and ‘:last’ is looking for the last table row of the table. a table using jQuery?

How do I find the last row in Excel VBA?

Code: Sub Example2 () Dim Last_Row As Long Last_Row = Cells (Rows.Count, 1) End Sub. This code allows VBA to find out the total number of (empty + non-empty) rows present in the first column of excel worksheet. This means this code allows the system to go to the last cell of Excel.

What is the difference between ‘TR’ and ‘last’ in a query?

The ‘table’ in query looks for the table element then ‘tr’ is looking for all the rows in the table element and ‘:last’ is looking for the last table row of the table. a table using jQuery?


2 Answers

The last row of a table can be accessed from the rows property.

var lastRow = table.rows[ table.rows.length - 1 ];

However I think there is some confusion as to what exactly you're asking for.

like image 75
mwcz Avatar answered Dec 15 '22 18:12

mwcz


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.

This example show cases some of the things 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 ); // 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;
</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 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 getElementById) and then select for the right row and column through rows and cells making sure to subtract 1 due to arrays indexing starting at 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]; // 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;
</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>
like image 45
user17753 Avatar answered Dec 15 '22 17:12

user17753