Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code> // 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) ); } </code></pre> <p>Also note that you should not do <em>DOM selection</em> in a loop. Cache it once outside the loop, and reference it in the loop as I did above.</p> <hr> <p>It seems that there have been some changes to the code from the original question. I'd rewrite the code like this:</p> <pre><code>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(); }); }); } </code></pre> <p>Now each <code>.click()</code> handler is referencing a unique <code>i</code>, which is the index of the current <code>$a_els</code> element in the iteration.</p> <p>So for example when a click happens on the <code>$a_els</code> at index <code>3</code>, <code>$items.eq(i).show();</code> will show the <code>$items</code> element that is also at index <code>3</code>.</p> <hr> <p>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.</p> <p>If you're using jQuery 1.7 or later, you'd use <code>.on()</code>...</p> <pre><code>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 &lt;a&gt; $items.hide(); $items.eq(idx).show(); // ...and use that index to show the content }); } </code></pre> <p>Or before jQuery 1.7, you'd use <code>.delegate()</code>...</p> <pre><code>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 &lt;a&gt;... $items.hide(); $items.eq(idx).show(); // ...and use that index to show the content }); } </code></pre> <p>This way there's only one handler bound to the <code>$tabCr</code> container. It check to see if the item clicked matches the <code>'a'</code> selector, and if so, it invokes the handler.</p> <p>If there are other elements in between the <code>&lt;a&gt;...&lt;/a&gt;</code> elements or the <code>&lt;div class="item"&gt;...&lt;/div&gt;</code> elements so that the indices don't naturally match up, we'd need to tweak the <code>.index()</code> call just a bit.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload