Skip to content

Instantly share code, notes, and snippets.

@phawk
Created February 8, 2012 12:02
Show Gist options
  • Select an option

  • Save phawk/1768516 to your computer and use it in GitHub Desktop.

Select an option

Save phawk/1768516 to your computer and use it in GitHub Desktop.
jQuery multiple autotabs
<script type="text/javascript">
var doc = {
init: function()
{
// Setup first group
doc.structureTabs('#tabs');
jQuery("#tabs").tabs();
// Setup second group
doc.structureTabs('#tabs2');
jQuery("#tabs2").tabs();
},
structureTabs: function(elementId)
{
// Insert a list to hold the tab buttons
$(elementId).prepend('<ul class="tabbtns"></ul>');
// Split the content up, starting with each h2 and all other content until the following h2 comes.
$(elementId+' h2').each(function(index){
$(this).nextUntil('h2').andSelf().wrapAll('<div id="tabs-'+(index+1)+'"></div>');
var title = $(this).text();
$(elementId+' .tabbtns').append('<li><a href="#tabs-' + (index+1) + '">' + title + '</a></li>');
});
// Remove the h2 headings.
$(elementId+' div h2').remove();
}
};
// Runs this function when the page loads
$(document).ready(doc.init);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment