user718229
user718229

Reputation: 573

return first element if none clicked jquery

Im working on a video thumb gallery. I couldnt figure out how to do it the other way...This works but i would like to return the first video by default. I think I have to add a true/false statement to show the first if none have been clicked? Thanks in advance.

$('.thumb').live('click', function() {
          $('.thumb').not(this).next().removeClass('showVid');
    if(!$(this).next().hasClass('showVid'))
    {

        $(this).next().addClass('showVid');

    }


});

Upvotes: 0

Views: 66

Answers (2)

charlietfl
charlietfl

Reputation: 171679

Question/description is a bit vague but sounds like you want to cycle the thumbs and return to beginning

$('.thumb').live('click', function() {
     $('.showVid').removeClass('showVid');
     var next=$(this).next();
     // if next doesn't exist, go to first
     next.length ? next.addClass('showVid')  : $('.thumb:first').addClass('showVid');
});

Upvotes: 1

Malitta N
Malitta N

Reputation: 3423

If you want the first .thumb to be selected on page load, add

$('.thumb:first').trigger('click');

after binding your handler.

Upvotes: 0

Related Questions