i have a jquery problem with lists. I have a big list and i want when a link is clicked; each time the next 5 items of that list will be shown and the previous items hide.
How can i do this?
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
<li>13</li>
<li>14</li>
<li>15</li>
Onload this should be shown:
1
2
3
4
5
when clicked on "next"
6
7
8
9
10
when clicked on "next"
11
12
13
14
15
Thanks in advance !
This solution is shorter, and also works bidirectional (Previous
and Next
).
Fiddle: http://jsfiddle.net/JQq5n/61/
$('ul li:gt(4)').hide();
$('.prev').click(function() {
var first = $('ul').children('li:visible:first');
first.prevAll(':lt(5)').show();
first.prev().nextAll().hide()
});
$('.next').click(function() {
var last = $('ul').children('li:visible:last');
last.nextAll(':lt(5)').show();
last.next().prevAll().hide();
});
Here's an option: http://jsfiddle.net/JQq5n/
Don't know how/if you plan to show the previous items so I've left that out
This is untested code, but I'm thinking it'll get you going in the right direction..
var currentShowingSet = 1;
$('#next').click(function() {
// Hide all.
$('ul li').hide();
// increment currently showing set of items.
currentShowingSet++;
// show the next 5 children of the list.
for(var i = 1; i < 6; i++) {
$('ul li:nth-child(' + (currentShowingSet * i) + ')').show();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With