Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery list show / hide 5 items onclick

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 !

like image 831
Yves Avatar asked Jul 09 '11 14:07

Yves


3 Answers

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();
});
like image 164
Rob W Avatar answered Oct 30 '22 22:10

Rob W


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

like image 5
Stuart Burrows Avatar answered Oct 30 '22 23:10

Stuart Burrows


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();
    }
});
like image 1
Kon Avatar answered Oct 30 '22 23:10

Kon