user1469270
user1469270

Reputation:

If last child, do something else

I'm looping through a series of images, passing the class .current along a series of elements, and cloning it each time. Like so:

http://jsfiddle.net/EMFZe/3/

Note: I'm using .img as a placeholder for img.

$('.next').click(function(){

    $('.main-frame').empty();
    if (!$('.slide-controller .slide.current').is(':last')) {

        $('.current')
        .removeClass('current')
        .next()
        .addClass('current')
        .clone('.img')
        .appendTo('.main-frame');
    } else { 
        alert('else!'); // this is never called
    }
});

Problem: The if statement does not seem to be working correctly: the else is never called. Would anyone know why?

(Eventually, when the .current is on the last child, I need to reverse the slideshow)

Upvotes: 1

Views: 129

Answers (3)

Billy Moat
Billy Moat

Reputation: 21050

I think I answered this first in the comments above but no biggie if you accept someone else's answer first.

Change :last to :last-child

http://jsfiddle.net/EMFZe/4/

Upvotes: 1

Alex
Alex

Reputation: 7374

Change last for last-child and all should be well:

http://jsfiddle.net/EMFZe/6/

if (!$('.slide-controller .slide.current').is(':last-child')) {

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You are not specifying last of what... try

$('.slide-controller .slide.current').is('.slide-controller .slide:last')

Demo: Fiddle

Or check for :last-child

$('.slide-controller .slide.current').is(':last-child')

Demo: Fiddle

Upvotes: 2

Related Questions