- Back to Home »
- jQuery »
Posted by : Jebastin
Wednesday, 19 February 2014
jQuery functionality to load first 5 elements & click "load more" to display next 5 elements.
HTML:
CSS:
jQuery:
HTML:
- <ul id="myList">
- <li>One</li>
- <li>Two</li>
- <li>Three</li>
- <li>Four</li>
- <li>Five</li>
- <li>Six</li>
- <li>Seven</li>
- <li>Eight</li>
- <li>Nine</li>
- <li>Ten</li>
- <li>Eleven</li>
- <li>Twelve</li>
- <li>Thirteen</li>
- <li>Fourteen</li>
- <li>Fifteen</li>
- <li>Sixteen</li>
- <li>Seventeen</li>
- <li>Eighteen</li>
- <li>Nineteen</li>
- <li>Twenty one</li>
- <li>Twenty two</li>
- <li>Twenty three</li>
- <li>Twenty four</li>
- <li>Twenty five</li>
- </ul>
- <div id="loadMore">Load more</div>
- <div id="showLess">Show less</div>
CSS:
- #myList li{
- display:none;
- }
- #loadMore {
- color:green;
- cursor:pointer;
- }
- #loadMore:hover {
- color:black;
- }
- #showLess {
- color:red;
- cursor:pointer;
- }
- #showLess:hover {
- color:black;
- }
jQuery:
- $(document).ready(function () {
- size_li = $("#myList li").size();
- x=3;
- $('#myList li:lt('+x+')').show();
- });
- $('#loadMore').click(function () {
- x= (x+5 <= size_li) ? x+5 : size_li;
- $('#myList li:lt('+x+')').show();
- });
- $('#showLess').click(function () {
- x=(x-5<0) ? 3 : x-5;
- $('#myList li').not(':lt('+x+')').hide();
- });