Created
December 17, 2012 19:51
-
-
Save wpscholar/4321419 to your computer and use it in GitHub Desktop.
List Expansion jQuery Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( 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> ' ); | |
} ); | |
$('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