I have this code :
//Pagination
pageSize = 8;
showPage = function(page) {
$(".line-content").hide();
$(".line-content").each(function(n) {
if (n >= pageSize * (page - 1) && n < pageSize * page)
$(this).show();
});
}
showPage(1);
$("#pagin li a").click(function() {
$("#pagin li a").removeClass("current");
$(this).addClass("current");
showPage(parseInt($(this).text()))
});
.current {
color: green;
}
#pagin li {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="line-content">1 I have some content</div>
<div class="line-content">2 I have some content</div>
<div class="line-content">3 I have some content</div>
<div class="line-content">4 I have some content</div>
<div class="line-content">5 I have some content</div>
<div class="line-content">6 I have some content</div>
<div class="line-content">7 I have some content</div>
<div class="line-content">8 I have some content</div>
<div class="line-content">9 I have some content</div>
<div class="line-content">10 I have some content</div>
<div class="line-content">11 I have some content</div>
<div class="line-content">12 I have some content</div>
<ul id="pagin">
<li><a class="current" href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
</ul>
Instead of writing numbers manually in my HTML file, I want the numbers generate automatically according to the number of divs to display.
The code I have works but there is nothing in page 3 and 4. Instead of update the numbers in my HTML file, I want them to change dynamically with Jquery
Place this somewhere it is executed when the DOM is ready, but before the click event handlers are added.
Then you can just leave
#pagin
empty and the JavaScript will fill it for you:Disclaimar: I haven't tested this code.
you can use these code for pagination if you will have wanted appending to tag in html .
You need to count the pages using
Math.ceil($(".line-content").size() / pageSize)
, and then dynamically add<li>
for each page.I have moved the initialization code inside $() (i.e. the Ready Event).
pageSize = 5;
}
calculate page count than via a loop create links to pages.