Joe Half Face
Joe Half Face

Reputation: 2333

Check if element is last of the class in the DOM

I have this code:

$('.bird').each(function(){
    $(this).load(function(){
      if($(this).is(".bird:last")){
        $('#floatingBarsG').remove();
        $('#step0').fadeIn(600);}
    });
})

Where I try to do some events if $(this) is last element on the page with class .bird, and this code doesn't work.

What is wrong?

Upvotes: 1

Views: 341

Answers (2)

DEMO

$('.bird').click(function () {
    if ($('.bird').length - 1 == $(this).index('.bird')) {
        alert('last')
    }
})

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Try

var $birds = $('.bird').each(function () {
    $(this).load(function () {
        if ($birds.last().is(this)) {
            $('#floatingBarsG').remove();
            $('#step0').fadeIn(600);
        }
    });
})

Upvotes: 3

Related Questions