Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery show next div of same class?

Tags:

jquery

I have a bunch of div's like

            <div class="slides">
                <div id="box_bg">
                    <div class="previous"></div>
                    <img src="images/minido_pic.png" width="267" height="252" alt="MiniDo" class="img1" />
                    <div class="next"></div>
                </div>
                <div id="text1">
                    <p>MiniDo - Adobe Air App - Simplistic design wrapped around a simple iOS style window built for the Windows environment using Adobe Air.</p>
                </div>
            </div>

and a bunch of jquery

$(document).ready(function(){   
    $(".slides").hide();

        var length = $(".slides").length;
        var ran = Math.floor(Math.random()*length);
        $(".slides:nth-child(" + ran + ")").show();

    $('.previous').click(function() {
        $(".slides").parentsUntil(".slides").prev().show();
        $('.slides').hide();
    });

    $('.next').click(function() {
        $(this).parentsUntil(".slides").next().show();
        $('.slides').hide();
    });
});

Now what I want to so is on page load, show a random ".slides" which works fine. And then when a user presses ".next" or ".previous" it loads the next ".slides".

However I can't seem to get it to work? When I press next or previous it shows nothing?

Any help?

like image 494
Steven Avatar asked Jun 06 '11 14:06

Steven


People also ask

How do you get next element in the same class?

To find the next element with specific class:Use the nextElementSibling to get the next element sibling. In a while loop iterate over the next siblings. Check if each element's class list contains the specific class.

What is .next in jQuery?

jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.

What is addClass in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


1 Answers

You should use

for previous

$(this).closest('.slides').prevAll('.slides').eq(0).show();

and for next

$(this).closest('.slides').nextAll('.slides').eq(0).show();

And the most important is to hide the .slides before you show the next one.

So the $('.slides').hide(); should be before the .show() commands otherwise, you just show the one you want and then hide all (including the one you just showed)


Altogether

$('.previous').click(function() {
    $('.slides').hide();
    var previous = $(this).closest('.slides').prevAll('.slides').eq(0);
    if (previous.length === 0) previous = $(this).closest('.slides').nextAll('.slides').last();
    previous.show();
});

$('.next').click(function() {
    $('.slides').hide();
    var next = $(this).closest('.slides').nextAll('.slides').eq(0);
    if (next.length === 0) next = $(this).closest('.slides').prevAll('.slides').last();
    next.show();
});
like image 92
Gabriele Petrioli Avatar answered Sep 27 '22 16:09

Gabriele Petrioli