I have a set of unordered list items that I want to display only the first 3 li
elements for each when loading. Upon clicking the +
sign, the remaining list items should toggle visibility for just that specific list group. While this can potentially be achieved with CSS alone, I am currently working on implementing it using jQuery.
Here is how I have set it up:
HTML
<div class="container">
<div class="col-lg-3">
<div class="toggle" tabindex="1">
<p class="header" onclick="void(0)">LINK</p>
<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>
<li>link 5</li>
<li>link 6</li>
</ul>
</div>
</div>
<div class="col-lg-3">
<div class="toggle" tabindex="1">
<p class="header" onclick="void(0)">LINK</p>
<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>
<li>link 5</li>
<li>link 6</li>
</ul>
</div>
</div>
<div class="col-lg-3">
<div class="toggle" tabindex="1">
<p class="header" onclick="void(0)">LINK</p>
<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>
<li>link 5</li>
<li>link 6</li>
</ul>
</div>
</div>
</div>
jQuery
$('ul li').hide().filter(':lt(3)').show();
$('ul').append('<li>+</li>').find('li:last').click(function() {
$(this).siblings(':gt(2)').toggle();
});
JSFIDDLE: LINK