Dronacharya
Dronacharya

Reputation: 481

How do I select the last element with a specified class from descendants of this?

I have a <div> with many <ul> descendants with different classes, and I am calling a function on this <div> which modifies the last <ul> with a given class. How do I select the last <ul> descendant with a specified class using $(this) jQuery selector?

Upvotes: 1

Views: 161

Answers (2)

erimerturk
erimerturk

Reputation: 4288

i think it should work also.

var uls = $(this).find("ul.someClass")

var lastUl = uls[uls.length - 1];

Upvotes: 0

James Allardice
James Allardice

Reputation: 165951

You can use the last method:

var last = $(this).find("ul.someClass").last();

Or the :last selector:

var last = $(this).find("ul.someClass:last");

Upvotes: 5

Related Questions