I’ve got some tabs setup; here’s what they currently do:
- hide content when page is opened
- show content on tab click (with a toggle so user can show/hide/show/hide the content)
- active tab changes color when selected
The issue: when the user clicks on one of the tabs and shows the content, and then clicks on the other tab, both sets of content show (like a cumulative toggle).
I want to set it up so that if the user clicks a tab and shows the content, and then clicks the other tab, the content showing for the first tab clicked will hide.
Here’s another SO question that deals with something similar, but it doesn’t include the active class code I have – Dealing with multiple toggles and the JS that works – http://jsfiddle.net/jHvjD/5/
JQUERY
$(document).ready(function(){
$('.tab_contents').hide();
$('.tab').click(function() {
$(this.rel).toggle();
$('#tabs_container > .tabs > li.active')
.removeClass('active');
$(this).parent().addClass('active');
$('#tabs_container > .tab_contents_container > div.tab_contents_active')
.removeClass('tab_contents_active');
$(this.rel).addClass('tab_contents_active');
});
});
HTML
<div id="tabs_container">
<!-- These are the tabs -->
<ul class="tabs">
<li>
<a href="#" rel="#tab_1_contents" class="tab">Option 1</a>
</li>
<li><a href="#" rel="#tab_2_contents" class="tab">Option 2</a></li>
</ul>
<div class="clear"></div>
<div class="tab_contents_container">
<!-- Tab 1 Contents -->
<div id="tab_1_contents" class="tab_contents tab_contents_active">Option 1 stuff </div>
<!-- Tab 2 Contents -->
<div id="tab_2_contents" class="tab_contents">Option 2 stuff</div>
</div>
fiddle : http://jsfiddle.net/GkGyt/2/