Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Showing/Hiding Lists Re: Showing/Hiding Lists

#86394
kalps1982
Member

hey!

check the cor below! its working with less and more both.

i have added one constant variable visible which will set how many are visible at once!

link to jsfiddle:

http://jsfiddle.net/9pSXR/

jQuery(function($) {
var visible = 7;
$('ul li:gt('+(visible - 1)+')').hide();

$('#more').click(function() {
if ($('ul li:visible:last').is(':last-child')) {
return false;
}

var currentIndex = $('ul').children('li:visible:last').index(),
nextIndex = currentIndex + (visible + 1);
$('ul li').hide();
$('ul li:lt(' + nextIndex + '):gt(' + currentIndex + ')').show();
});

$('#less').click(function() {
if ($('ul li:visible:first').is(':first-child')) {
return false;
}

var currentIndex = $('ul').children('li:visible:first').index();
var prevIndex = (currentIndex - (visible + 1));
if(prevIndex < 0) {
prevIndex = 0;
}

$('ul li').hide();
if(prevIndex == 0)
$('ul li:lt(' + currentIndex + ')').show();
else
$('ul li:lt(' + currentIndex + '):gt(' + prevIndex + ')').show();
});
});