King Kong
King Kong

Reputation: 2915

How to find the last child of an element?

 <div id="main">
  <div class="1"></div>
  <div class="2"></div>
  <div class="2"></div>
  <div class="3"></div>
  <div class="3"></div>
  <div class="4"></div>
 </div>

How we can write a selector using :last-child in jquery to find out the last div with class 3 ?

Upvotes: 3

Views: 433

Answers (4)

Esailija
Esailija

Reputation: 140234

Try this selector... though your classes shouldn't be just numbers. They should start with a normal character.

$("#main .3:last")

FYI, :last-child checks if the target is last child of its parent. None of the elements with class .3 are the last child of their parent.

Upvotes: 12

Darren
Darren

Reputation: 70796

Use .last():

$("#main .3").last();

http://api.jquery.com/last/

http://jsfiddle.net/E64Wm/1/

Upvotes: 4

slash197
slash197

Reputation: 9034

$('#main > .3').last() should point to the right element

Upvotes: 0

Max Allan
Max Allan

Reputation: 640

Try this:

$('#main').find(':last-child');

Upvotes: -5

Related Questions