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?
Asked
Active
Viewed 147 times
1
Seth
- 528
- 3
- 16
- 32
Dronacharya
- 471
- 1
- 4
- 15
2 Answers
5
You can use the last method:
var last = $(this).find("ul.someClass").last();
Or the :last selector:
var last = $(this).find("ul.someClass:last");
James Allardice
- 164,175
- 21
- 332
- 312
-
Thanks for the prompt reply, I will try it out :). – Dronacharya Nov 30 '11 at 08:44
-
2You can also read about alternate ways to select child elements at http://stackoverflow.com/a/306904/896341 – Stefan Nov 30 '11 at 08:46
0
i think it should work also.
var uls = $(this).find("ul.someClass")
var lastUl = uls[uls.length - 1];
erimerturk
- 4,230
- 25
- 25
-
It would, but `length` is a property, not a method. `size` is a method though, so you could use that. – James Allardice Nov 30 '11 at 08:45