Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created December 17, 2012 19:51
Show Gist options
  • Save wpscholar/4321419 to your computer and use it in GitHub Desktop.
Save wpscholar/4321419 to your computer and use it in GitHub Desktop.
List Expansion jQuery Plugin
( function( $ ) {
$.fn.listExpansion = function( options ) {
var settings = {
expandLinkHTML : '+', // Set the HTML to display inside the 'Expand' link
collapseLinkHTML : '-' // Set the HTML to display inside the 'Collapse' link
};
return this.each( function() {
// If options exist, lets merge them with defaults
if ( options ) {
$.extend( settings, options );
}
var $wrap = $(this);
var $parents = $wrap.children('li');
// Hide all children
$('ul', $parents).hide();
// Add a class to indicate that lists are collapsed
$parents.addClass('list-collapsed');
// Add buttons
$parents.each( function( i, el ) {
$(el).prepend( '<a href="#" class="list-expansion-link">' + settings.expandLinkHTML + '</a>&nbsp;' );
} );
$('a.list-expansion-link', $wrap).bind({
'click': function(e) {
e.preventDefault();
var $child = $(this).siblings('ul').first();
$child.slideToggle().parent().toggleClass('list-collapsed').toggleClass('list-expanded');
if( $child.is(':visible') ) {
$(this).html( settings.expandLinkHTML );
} else {
$(this).html( settings.collapseLinkHTML );
}
}
});
}) ;
};
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment