Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector to last row first column

I'm trying to change the value of the first cell on the last row of a table.

My code is:

$(document).ready(function() {
    $('table.class tr:last td:first').html('Value');     
});

But that code changes nothing, but if I put without the :last and :first he fills the all table with 'Value'. What am I doing wrong?

Edit: My bad, the code works fine, but just for the last table with class 'class'.What I need it's to do that on every tables with that class. Any idea?

like image 477
rnunes Avatar asked May 01 '11 19:05

rnunes


People also ask

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

Use find() method to find the all table rows of the table. Use last() method to get the last row of table.

How do I select a specific row in a table using jQuery?

In order to use jQuery in the HTML file, we will add the below syntax inside the <head> tag. Syntax for selecting odd rows: $("table tr:odd").

How do you insert 3 rows before the last row of a table?

Click in a cell above or below where you want to add a row. Under Table Tools, on the Layout tab, do one of the following: To add a row above the cell, click Insert Above in the Rows and Columns group. To add a row below the cell, click Insert Below in the Rows and Columns group.


2 Answers

For your edit, use :last-child and :first-child instead so it applies to the last td in the last tr of every table.class:

$(document).ready(function() {
    $('table.class tr:last-child td:first-child').html('Value');
});
like image 69
BoltClock Avatar answered Sep 25 '22 05:09

BoltClock


The code works fine.

See http://jsfiddle.net/ThiefMaster/sJGZj/

like image 43
ThiefMaster Avatar answered Sep 21 '22 05:09

ThiefMaster