Saleh Alomari
Saleh Alomari

Reputation: 53

how to use same jQuery function on multiple group of lists

I have Unordered list, and each item of the list has a list of images. I wrote a jQuery function to operate slider using prev and next. The function working write when there is only one list of images. Nonetheless, when I have list and each item has list of images when ever I clicked on any slider only the first slider is working regardless on which slider I clicked. my question is, How can I modify the jQuery function to operate only for the slider that I clicked on and not other sliders for other group of images.

Here is the html code:

<ul class="results">
    <li class="result-row">
        <!-- image box -->

        <a href="#">
            <div class="result-image-act" >
                <img  src="1.jpg" class="active">
                <img  src="2.jpg">

                <img  src="3.jpg">
                <img  src="1.jpg">
                <img  src="2.jpg">
                <img  src="3.jpg">
                <img  src="1.jpg">
                <img  src="2.jpg">
                <img  src="3.jpg">
            </div>

            <div class="swipe-wrap">
                <div class="swipe-wrap-lef">
                    <a  href="#">
                        <div class="swipe-prev">
                            <p>&lt;</p>
                        </div>
                    </a>
                </div>
                <div class="swipe-wrap-rig">
                    <a  href="#">
                        <div class="swipe-next">
                            <p>&gt;</p>
                        </div>
                    </a>
                </div>
            </div>
        </a>
    </li>

                        <!--  -->
    <li class="result-row">
        <a href="#">

            <div class="result-image-act">
            <img  src="1.jpg" class="active">
            <img  src="2.jpg">
            <img  src="3.jpg">
            <img  src="1.jpg">
            <img  src="2.jpg">
            <img  src="3.jpg">
            <img  src="1.jpg">
            <img  src="2.jpg">
            <img  src="3.jpg">
            </div>

            <div class="swipe-wrap">
                <div class="swipe-wrap-lef">
                    <a  href="#">
                        <div class="swipe-prev">
                            <p>&lt;</p>
                        </div>
                    </a>
                </div>
                <div class="swipe-wrap-rig">
                    <a  href="#">
                        <div class="swipe-next">
                            <p>&gt;</p>
                        </div>
                    </a>
                </div>
            </div>
        </a>
    </li>
</ul>

Here is the jQuery function:

$(document).ready(function() {


    $('.swipe-prev').click(function(){
    console.log("Prev");    // Print "Prev" on click swipe-prev
    var prevImg = $('img.active').prev('.result-image-act img');
    if(prevImg.length == 0) {
      prevImg = $('.result-image-act img:last');
    }
    $('img.active').removeClass('active');
    prevImg.addClass('active');
    });

    $('.swipe-next').click(function() {
    console.log("Next");  // Print "Next" on click swipe-next
    var nextImg = $('img.active').next('.result-image-act img');
    if(nextImg.length == 0) {
      nextImg = $('.result-image-act img:first');
    }
    $('img.active').removeClass('active');
    nextImg.addClass('active');
    });

});

Upvotes: 0

Views: 49

Answers (2)

gaetanoM
gaetanoM

Reputation: 42054

You may reduce everything to only one event handler.

$('.swipe-prev, .swipe-next').on('click', function (e) {
    var op = ($(this).is('.swipe-prev')) ? 'prev' : 'next';
    var firtOrlast = ($(this).is('.swipe-prev')) ? 'last' : 'first';
    var imgList = $(this).closest('.result-row');
    var currImg = imgList.find('.result-image-act img.active');
    var nextImg = currImg[op]();
    if (nextImg.length == 0) {
        nextImg = imgList.find('.result-image-act img:' + firtOrlast);
    }
    currImg.removeClass('active');
    nextImg.addClass('active');
});
.active {
    padding: 3px;
    border: 1px solid #021a40;
    background-color: #ff0;
}
.swipe-wrap {
    display: inline-flex;
}
.swipe-wrap > div {
    padding-right: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<ul class="results">
    <li class="result-row">
        <!-- image box -->

        <a href="#">
            <div class="result-image-act">
                <img src="https://dummyimage.com/100x100/000/fff&text=1" class="active">
                <img src="https://dummyimage.com/100x100/000/fff&text=2">
                <img src="https://dummyimage.com/100x100/000/fff&text=3">
                <img src="https://dummyimage.com/100x100/000/fff&text=4">
            </div>

            <div class="swipe-wrap">
                <div class="swipe-wrap-lef">
                    <a href="#">
                        <div class="swipe-prev">
                            <p>&lt;</p>
                        </div>
                    </a>
                </div>
                <div class="swipe-wrap-rig">
                    <a href="#">
                        <div class="swipe-next">
                            <p> &gt;</p>
                        </div>
                    </a>
                </div>
            </div>
        </a>
    </li>

    <!--  -->
    <li class="result-row">
        <a href="#">

            <div class="result-image-act">
                <img src="https://dummyimage.com/100x100/000/fff&text=1" class="active">
                <img src="https://dummyimage.com/100x100/000/fff&text=2">
                <img src="https://dummyimage.com/100x100/000/fff&text=3">
                <img src="https://dummyimage.com/100x100/000/fff&text=4">
            </div>

            <div class="swipe-wrap">
                <div class="swipe-wrap-lef">
                    <a href="#">
                        <div class="swipe-prev">
                            <p>&lt;</p>
                        </div>
                    </a>
                </div>
                <div class="swipe-wrap-rig">
                    <a href="#">
                        <div class="swipe-next">
                            <p>&gt;</p>
                        </div>
                    </a>
                </div>
            </div>
        </a>
    </li>
</ul>

Upvotes: 1

Daniel
Daniel

Reputation: 3370

Your problem is coming from how you find the active image. There are presumably multiples, but you want to find the one related to whatever was clicked. Change lines like this:

$('img.active')

to something like:

$(this).closest("ul.results").find("img.active")

to get the img.active within the clicked ul.

Upvotes: 1

Related Questions