Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple jQuery Tabs
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="tabs.js"></script>
</head>
<body>
<div class="tab-container">
<ul class="tabs">
<li><a href="#tab1">Tab 1</a></li>
<li><a href="#tab2">Tab 2</a></li>
<li><a href="#tab3">Tab 3</a></li>
</ul><!-- .tabs -->
<div class="tabbed-content-container">
<div class="tab-content" id="tab1">
<h3>Hello, I'm Tab 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><!-- .tab-content -->
<div class="tab-content" id="tab2">
<h3>Hello, I'm Tab 2</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><!-- .tab-content -->
<div class="tab-content" id="tab3">
<h3>Hello, I'm Tab 3</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><!-- .tab-content -->
</div><!-- .tabbed-content-container -->
</div><!-- .tab-container -->
$(function() {
$('.tabs').each(function(index) {
// Create an array for the element selectors that are to be targeted
// They are the divs that show and hide
var target_selectors = [];
// Loop over the triggers in the tab menu
// The triggers are the <a /> elements in the menu
var $triggers = $('a', $(this)).each(function(index) {
// get their href attribute which is the target id
// eg: href="#target1" -> <div id="target1" />
// Push the selector into the array
target_selectors.push(this.hash);
})
// when we click the link...
// or the click event is fired
.click(function() {
// remove the active class from all the triggers
$triggers.parent().removeClass("active");
// add the active class to the selected trigger
$(this).parent().addClass("active");
$targets.hide();
// If the anchor href hash is #show_all -> a href="#show_all"
if((/show_all$/).test(this.hash))
{
// fade in all the targets
$targets.fadeIn();
}
// else we're tageting something sepcific
else
{
// fade in the target
$targets.filter(this.hash).fadeIn();
}
return false;
});
// Join the selectors: "#target1, #target2, #target3"
// create a jquery object from the selectors
// hide the selected elements (the targets)
// return the jQuery collection of the targets
var $targets = $(target_selectors.join(", ")).hide();
// click the first trigger
// which is the first anchor
// this fires the click event above
$triggers.eq(0).trigger('click');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment