Skip to content

Instantly share code, notes, and snippets.

@BinaryKitten
Created July 16, 2014 10:52
Show Gist options
  • Select an option

  • Save BinaryKitten/452a79975d7fe29301da to your computer and use it in GitHub Desktop.

Select an option

Save BinaryKitten/452a79975d7fe29301da to your computer and use it in GitHub Desktop.
jQuery List Toggler
<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>
(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