Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Get last row of table within last element in a series

I have a series of slides based off of sections:

<div id="slides">
    <section id="first">
        <section>
            <table>
                <thead>
                </thead>
                <tbody>
                    <tr id="somethingUnique">
                    ...
                    </tr>
                </tbody>
            </table>
        </section>
        <section>
            <table>
                <thead>
                </thead>
                <tbody>
                    <tr id="somethingUnique">
                    ...
                    </tr>
                </tbody>
            </table>
        </section> 
       ...
    </section>
</div>

I need to select grab the ID of the last row from the table in the last section of #first section.

I'm using the following Jquery, getting "undefined" back...any ideas?

    var lastListItem = $('#first:last-child table>tbody>tr:last').attr("id");
    alert(lastListItem);
like image 714
apttap Avatar asked Oct 08 '12 17:10

apttap


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 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.

How do I get the first row of a table in jQuery?

Select the first row in a table $("tr:first") in jQuery.

What is jQuery last?

The last() method returns the last element of the selected elements.


2 Answers

$('#first table:last tr:last')

or:

$('#first tr:last')

http://jsfiddle.net/hunter/QMzHH/

like image 58
hunter Avatar answered Oct 14 '22 05:10

hunter


var lastListItem = $("#first").find("section").last().find("tr").last().attr("id");

I prefer using [0].id instead of .attr("id") since its one less method call; however, if you're not positive that you'll always have a table in that DOM position, attr is safer.

like image 34
Joe Johnson Avatar answered Oct 14 '22 06:10

Joe Johnson