Question

I'm creating a pagination that loads pages into a div using jQuery load method. I would like to add a next and previous button that would allow users to load either the next or previous page when clicked into div "#content". I've looked into jQuery .prev() and .next() methods but I don't know how I'd incorporate those methods here to achieve my goal. Any suggestions or demos highly welcome. Thank You in advance.

//References
var sections = $("a"),
   pageBody = $("#content"); //new/prev pages load in this div

//Manage click events
sections.on('click', function () {

   //load selected section
   switch (this.id) {
   case "page-1":
      pageBody.load("page1.html");
      break;
   case "page-2":
      pageBody.load("pagee2.html");
      break;
   case "page-3":
      pageBody.load("page3.html");
      break;
   case "page-4":
      pageBody.load("page4.html");
      break;
   case "page-5":
      pageBody.load("page5.html");
      break;
   default:
      break;
   }
});

This is my HTML:

<div id="content">

   <h2>Page TITLE</h2>
   <p>Page Text Goes Here</p>


   <div>
      <a class="prev" href="#"></a>
      <ul class="nav">
         <li>
            <a id="page-1" href="#"></a>
         </li>
         <li>
            <a id="page-2" href="#"></a>
         </li>
         <li>
            <a id="page-3" href="#"></a>
         </li>
         <li>
            <a id="page-4" href="#"></a>
         </li>
         <li>
            <a id="page-5" href="#"></a>
         </li>
      </ul>
      <a class="next" href="#"></a>
   </div>

</div>
Was it helpful?

Solution

Change the markup to

<div id="content">
    <h2>Page TITLE</h2>
    <p>Page Text Goes Here</p>

    <div> 
        <a class="prev" href="#"></a>

        <ul class="nav">
            <li><a id="page1" href="#"></a></li>
            <li><a id="page2" href="#"></a></li>
            <li><a id="page3" href="#"></a></li>
            <li><a id="page4" href="#"></a></li>
            <li><a id="page5" href="#"></a></li>
        </ul> 

        <a class="next" href="#"></a>

    </div>
</div>

and use something like this

var sections = $(".nav a");

sections.on('click', function () {
    $("#content").load(this.id + '.html');
    sections.removeClass('active');
    $(this).addClass('active');
});

$('.next').on('click', function() {
    var idx = sections.index( sections.filter('.active') ) + 1;
    idx = idx > sections.length-1 ? 0 : idx;
    sections.eq(idx).trigger('click');
});

$('.prev').on('click', function() {
    var idx = sections.index( sections.filter('.active') ) - 1;
    idx = idx < 0 ? sections.length-1 : idx;
    sections.eq(idx).trigger('click');
});

it increments the index and gets the next / previous element etc.

OTHER TIPS

Give your #content div a data attribute and then use that as the current page e.g.

        <div id="content" data-nav="1">
            <h2>Page TITLE</h2>
            <p>Page Text Goes Here</p>
        <div>

            <a class="prev" href="#"></a>
            <ul class="nav">
                <li><a data-nav="1" href="#"></a></li>
                <li><a data-nav="2" href="#"></a></li>
                <li><a data.nav="3" href="#"></a></li>
                <li><a data.nav="4" href="#"></a></li>
                <li><a data.nav="5" href="#"></a></li>
            </ul>
            <a class="next" href="#"></a>
        </div>

Then your script can be something like this

//References
var sections = $("a"),
    pageBody = $("#content"),//new/prev pages load in this div
    prev = $(".prev"),
    next = $(".next");

//Manage click events
sections.on('click',changeSection(this.data('nav')));
prev.on('click',function() {
    // get the nav id
    navId = this.data('nav');
    prevId = navId - 1;

   //change the data
   changesection("page-" + prevId);
});


next.on('click',function() {
    // get the nav id
    navId = this.data('nav');
    prevId = navId + 1;

   //change the data
   changesection("page-" + prevId);
});

// You can place this function outside of the $(document).load 
function changeSection(id) {
 function(){
     // set the new nav
    pageBody.data('nav', id);
    //load selected section
    switch(id){
        case "1": 
                pageBody.load("page1.html");
                break;
        case "2": pageBody.load("pagee2.html");
                break;
        case "3": pageBody.load("page3.html");
                break;
        case "4": pageBody.load("page4.html");
                break;
        case "5": pageBody.load("page5.html");
                break;
        default:
                break;
    }
}

You can check out more info on .data() here

This is a quick thing I've done, there is definitely scope to make this better by refactoring the previous and next clicks and combining them into one function (by passing a true/false to add one or take one away) and you could potentially ditch the switch and load the page via a variable from the nav directly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top