At the moment I’m using a pager that when you click the page numbers loads the page. At the moment the code displays the current 10 closest pages and then arrows to go first, previous, next and to the last pages.
My aim is to create jQuery to dynamically update the #pages div with the correct numbers and/or remove the arrows if the user is on the first or last page.
HTML
<div id="pages">
<a href="1" class="first">«</a>
<a href="1" class="previous">‹</a>
<span class="current">2</span>
<a href="3">3</a>
<a href="4">4</a>
<a href="5">5</a>
<a href="6">6</a>
<a href="7">7</a>
<a href="8">8</a>
<a href="9">9</a>
<a href="10">10</a>
<a href="11">11</a>
<a href="3" class="next">›</a>
<a href="128" class="last">»</a>
</div>
Example jQuery for highest number click
//change page numbers if highest number clicked
$(document).on("click", "#pages a", function(){
if($(this).index() == 11){
$("#pages :nth-child(3)").remove();
$("#pages a:nth-child(11)").after('<a href="'+ (parseInt($(this).text()) + 1) + '" rel="nofollow" class="current">'+ (parseInt($(this).text()) + 1) + '</a>');
}
});
Here’s a jFiddle with a working example of what I currently have:
You can see in the jFiddle example that if you click the lower arrow or lowest number then the lowest number is removed and a new high number is inserted. The comments explain how this bit works.
The Problem
The current approach is kind of buggy and I’m not very happy with how it functions, is there a more reliable way to do what I’m looking for?
I played with your JSFiddle and modified the HTML to
defined some functions
and then set the
click()s individuallyComplete JSFiddle