Skip to content

Instantly share code, notes, and snippets.

@djs070
Created November 7, 2012 06:42
Show Gist options
  • Save djs070/4029875 to your computer and use it in GitHub Desktop.
Save djs070/4029875 to your computer and use it in GitHub Desktop.
jQuery tabs script
// Define elements
var $tabContainer = $('.tab-container');
var tabContentClass = '.tab-content';
var tabHeadingClass = '.tab-heading';
// Hide all tab content
$tabContainer.find(tabContentClass).hide();
// When tab button is clicked...
$tabContainer.find(tabHeadingClass).click(function(){
// Remove styling class
$(tabHeadingClass).removeClass('active');
// Hide all tabs
$tabContainer.find(tabContentClass).hide();
// Set active styling class on the clicked heading, and show the tab whose
// selector is contained in the heading's data-fortab attribute
$($(this).addClass('active').attr('data-fortab')).show();
});
// Activate the first tab by default
$tabContainer.find(tabHeadingClass).first().click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment