Created
July 16, 2014 10:52
-
-
Save BinaryKitten/452a79975d7fe29301da to your computer and use it in GitHub Desktop.
jQuery List Toggler
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
| <div class="speech-l"> | |
| <h4>Brands</h4> | |
| <ul> | |
| <li><a class="all" href="#">Hello</a> | |
| </li> | |
| <li><a class="all" href="#">Joe Rocket <em>(57)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Joe Rocket <em>(57)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Joe Rocket <em>(57)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Joe Rocket <em>(57)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Joe Rocket <em>(57)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Joe Rocket <em>(57)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Icon <em>(42)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Fieldsheer <em>(37)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Tour Master <em>(21)</em></a> | |
| </li> | |
| <li><a class="all" href="#">AGV Sport<em>(21)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Alpinestars<em>(21)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Cortech<em>(21)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Element<em>(21)</em></a> | |
| </li> | |
| <li><a class="all" href="#">Fieldsheer<em>(21)</em></a> | |
| </li> | |
| </ul> | |
| </div> |
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 ($, undefined) { | |
| $('.speech-l').each(function () { | |
| var ul = $('ul', this), | |
| items = ul.children(), | |
| min = ul.attr('data-min'); | |
| if (min === undefined) { | |
| min = 3; | |
| } | |
| if (items.length <= min) { | |
| return false; | |
| } | |
| var hiddenElements = items.slice(min).hide(), | |
| showCaption = 'View ' + hiddenElements.length + ' more comments...', | |
| hideCaption = 'View ' + hiddenElements.length + ' less comments...'; | |
| $('<li>', { | |
| text: showCaption, | |
| css: { | |
| cursor: 'pointer' | |
| }, | |
| click: function () { | |
| hiddenElements.toggle(888); | |
| $(this).text(function (idx, oldText) { | |
| return (oldText == showCaption) ? hideCaption : showCaption; | |
| }); | |
| } | |
| }).appendTo(ul); | |
| }); | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment