$(document).ready(function () { // If accordion heading type not defined, default to 'h2' if (typeof headingTag === 'undefined') { var headingTag = 'h2'; }; // Hide content divs $('#accordion div').hide(); // Add anchors to create links and tabbable items $('#accordion ' + headingTag).wrapInner(''); // Add required CSS classes to headers and content $('#accordion ' + headingTag).addClass('accordionDefault'); $('#accordion div').addClass('accordionContent'); // Add ARIA attributes // Accordian role (tablist) $('#accordion').attr('role','tablist'); // headingTag role (tab) $('#accordion ' + headingTag + " a").attr('role','tab'); // Accordian panel (tabpanel) $('#accordion .accordionContent').attr({ 'role': 'tabpanel', 'aria-hidden': 'true' }); $('#accordion ' + headingTag).each(function(i) { var count = i + 1; thePanel = 'panel' + count; theTab = 'tab' + count; $(this).find('a').attr({ id: theTab, 'aria-controls': thePanel }); $(this).next('div').attr({ id: thePanel, 'aria-labeledby': theTab }); }); // Add spacer at top of content to negate text animating $('#accordion div').prepend('
'); // Toggle function to change style and show/hide content $('#accordion ' + headingTag).click(function() { var thePanel = $(this).next('div'); $(this).toggleClass('accordionActive'); thePanel.slideToggle('fast'); if ($(this).hasClass('accordionActive')) { $(this).find('a').attr('aria-expanded','true'); thePanel.attr('aria-hidden','false'); } else { $(this).find('a').attr('aria-expanded','false'); thePanel.attr('aria-hidden','true'); } }); // Change heading style on keyboard focus $('#accordion '+ headingTag +' a').focus( function() { $(this).parent().addClass('accordionFocus') }); $('#accordion '+ headingTag +' a').focusout( function() { $(this).parent().removeClass('accordionFocus') }); // Prevent anchor from jumping to top of the page $('#accordion '+ headingTag +' a').click(function(e) { e.preventDefault() }); // Append show all, hide all controls $('Expand all sections / collapse all sections').prependTo('#accordion'); $('#expandAllSections').click(function(e) { // Show all items thePanel = $('#accordion ' + headingTag).next('div'); thePanel.each(function() { theHeading = $(this).prev(headingTag); theHeading.addClass('accordionActive'); theHeading.find('a').attr('aria-expanded','true'); thePanel.attr('aria-hidden','false'); $(this).show(); }); e.preventDefault(); }); $('#collapseAllSections').click(function(e) { // Hide all items thePanel = $('#accordion ' + headingTag).next('div'); thePanel.each(function() { theHeading = $(this).prev(headingTag); $(this).hide(); theHeading.removeClass('accordionActive'); theHeading.find('a').attr('aria-expanded','false'); thePanel.attr('aria-hidden','true'); }); e.preventDefault(); }); });