Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:57+00:00 2026-05-28T03:17:57+00:00

carousel: function(){ var $carouselCr = $(‘#carousel’), $tabCr = $(‘.carouselTabs’, $carouselCr), $itemCr = $(‘.carouselContents’, $carouselCr),

  • 0
carousel: function(){
            var $carouselCr = $('#carousel'),
                $tabCr = $('.carouselTabs', $carouselCr),
                $itemCr = $('.carouselContents', $carouselCr),
                tabAmount = (function(){
                    if($('a', $tabCr).length === $('.item', $itemCr).length){
                        return $('a', $tabCr).length;
                    }else{
                        throw "error: verschillend aantal tabs vs items";
                    }               
                })();

            var i = tabAmount;
            while(i--){                                     
                var item = $($('.item', $itemCr)[i]),
                    tab = $($('a', $tabCr)[i]);
                console.log(item, tab);
                $(tab).click(function(){
                    $('.item', $itemCr).hide();
                    $(item).show();
                })

            }

        }

As you can see, i’m trying to attach a click event to each ‘tab’, to select each ‘item’. I’m doing something wrong. All the tabs refer to the first item.

If i log $('.item', $itemCr)[i] inside the loop it will return all the different items, not just the first.

Simplified HTML structure

<div id="carousel" class="block">
    <div class="carouselTabs">
        <a href="#">
        </a>
    <!-- repeating -->
    </div>
    <div class="carouselContents">                      
        <div class="item">
        </div>  
    <!-- repeating -->                  
    </div>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:17:57+00:00Added an answer on May 28, 2026 at 3:17 am

    A loop doesn’t create a new variable scope. You need to create the click handler in a separate function, and pass whatever needs to be scoped into that function.

     // creates the handler with the scoped item, and returns the handler
    function create_handler(this_item) {
        return function () {
            $('.item', $itemCr).hide();
            $(this_item).show();
        };
    }
    
    var i = tabAmount;
    var a_els = $('a', $tabCr);
    var items = $('.item', $itemCr);
    while (i--) {
        var item = items[i],
            tab = a_els[i];
        $(tab).click( create_handler(item) );
    }
    

    Also note that you should not do DOM selection in a loop. Cache it once outside the loop, and reference it in the loop as I did above.


    It seems that there have been some changes to the code from the original question. I’d rewrite the code like this:

    carousel: function(){
        var $carouselCr = $('#carousel'),
            $tabCr = $('.carouselTabs', $carouselCr),
            $itemCr = $('.carouselContents', $carouselCr),
            $items = $('.item', $itemCr),
            $a_els = $('a', $tabCr);
    
        if($a_els.length !== $items.length)
            throw "error: verschillend aantal tabs vs items";
    
        $a_els.each(function(i) {
            $(this).click(function() {
                $items.hide();
                $items.eq(i).show();
            });
        });
    }
    

    Now each .click() handler is referencing a unique i, which is the index of the current $a_els element in the iteration.

    So for example when a click happens on the $a_els at index 3, $items.eq(i).show(); will show the $items element that is also at index 3.


    Another approach is to use event delegation, where you place the handler on the container, and provide a selector to determine if the handler should be invoked.

    If you’re using jQuery 1.7 or later, you’d use .on()…

    carousel: function(){
        var $carouselCr = $('#carousel'),
            $tabCr = $('.carouselTabs', $carouselCr),
            $itemCr = $('.carouselContents', $carouselCr),
            $a_els = $('a', $tabCr),
            $items = $('.item', $itemCr);
    
        if($a_els.length !== $items.length)
            throw "error: verschillend aantal tabs vs items";
    
        $tabCr.on('click','a',function() {
            var idx = $a_els.index( this ); // get the index of the clicked <a>
            $items.hide();
            $items.eq(idx).show(); // ...and use that index to show the content
        });
    }
    

    Or before jQuery 1.7, you’d use .delegate()…

    carousel: function(){
        var $carouselCr = $('#carousel'),
            $tabCr = $('.carouselTabs', $carouselCr),
            $itemCr = $('.carouselContents', $carouselCr),
            $a_els = $('a', $tabCr),
            $items = $('.item', $itemCr);
    
        if($a_els.length !== $items.length)
            throw "error: verschillend aantal tabs vs items";
    
        $tabCr.delegate('a','click',function() {
            var idx = $a_els.index( this ); // get the index of the clicked <a>...
            $items.hide();
            $items.eq(idx).show(); // ...and use that index to show the content
        });
    }
    

    This way there’s only one handler bound to the $tabCr container. It check to see if the item clicked matches the 'a' selector, and if so, it invokes the handler.

    If there are other elements in between the <a>...</a> elements or the <div class="item">...</div> elements so that the indices don’t naturally match up, we’d need to tweak the .index() call just a bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to create a simple jquery carousel animation: $(document).ready(function() { var
I have the following code (function($) { // carousel var Carousel = { settings:
I have this carousel function: if ($.browser.msie && $.browser.version.substr(0,1)<9) { $(ul.display li:eq(0))).show(); } else
I have this javascript code: $(function(){ var currentCarouselItem = 1; //set carousel to first
I'm trying to make a autoscrolling/carousel like function for an unordered list of images.
I recently tried to create an object like this: var carousel = { $slider:
I have 2 variables set like this: var carousel = $('div.carousel ul'); var pics
I have an infinite Carousel function applied to a series of images here: http://nokkam.com/showcase.html
var carousel = jQuery('#mycarousel').data('jcarousel'); var index = carousel.size() + 1; carousel.size(index); var html =
This works fine: $.getJSON(http://localhost:59396/xxxWeb/ CarouselHandler.ashx?action=getproducts&ids= + ids, function(data) { carousel.size(allProductIDs.length); if (numberOfImagesLeftToShow < numberOfImagesToDisplay)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.