Currently, my website uses a pager that loads the page when you click on the page numbers. The code shows the closest 10 pages along with arrows to navigate to the first, previous, next, and last pages.
I am aiming to develop jQuery code that can update the content of the #pages
div dynamically with the correct page numbers and remove the navigation 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>
Sample jQuery for highest number click event
//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>');
}
});
Check out this jFiddle link for a live demonstration:
In the jFiddle example, clicking the lower arrow or lowest number removes the lowest number and inserts a new higher number. The provided comments explain how this functionality works.
The Challenge
The current implementation has some bugs and does not meet my satisfaction in terms of functionality. I'm exploring better alternatives to achieve what I have envisioned. Is there a more reliable method to accomplish my goal?