Reputation: 498
hey, i'm wanting to create a basic pagination affect.. i have (lets say) 100 posts. i want to show the first 9, so therefore hide from 10 - 100 how do i grab those children.
my next request to have in mind is obviously to hide 1-9 show 10-18 hide 19-100 you get the idea. thanks in advanced.
mark up along the lines of:
<div class="grid">
<div class="widget">some content...</div>
<div class="widget">some content...</div>
<div class="widget">some content...</div>
<div class="widget">some content...</div>
<div class="widget">some content...</div>
<div class="widget">some content...</div>
etc...
</div>
Upvotes: 0
Views: 245
Reputation: 532435
You can use the slice function to restrict the selection to a range. Note that it's zero-based.
$('.widget').hide().slice(9,17).show();
Upvotes: 1
Reputation: 318478
Here's some code. Obviously you'll want to set page
and then execute the each()
code whenever the user changes the page.
var ITEMS_PER_PAGE = 2;
var page = 1;
// Option 1
$('.grid > .widget').each(function(i, item) {
var visible = i >= (ITEMS_PER_PAGE * (page - 1)) && i < (ITEMS_PER_PAGE * page);
$(item).toggle(visible);
});
// Option 2 (based on other answers)
$('.grid > .widget').hide().slice((ITEMS_PER_PAGE * (page - 1)), (ITEMS_PER_PAGE * page)).show();
Upvotes: 0