user2109326
user2109326

Reputation: 123

Jquery Mobile: Add next and previous buttons

I am working on a project that will have hundreds of pages into on html page.

I am very concern about navigating this sub-pages using <a href=#ID" > to move forward between sub-pages. Is there a way to use one button into every sub-page that will take me to the next sub-page in a way that I dont have to write every sub-page ID into every "Next" button ?

Upvotes: 3

Views: 4725

Answers (2)

FJ-web
FJ-web

Reputation: 143

I solved it this way:

$(document).ready(function() {

    $('.nextBtn').click(function() {
        $page = $(this).closest('div[data-role="page"]').next();
        $.mobile.changePage( $page, { transition: "slide", changeHash: false });
    });

    $('.prevBtn').click(function() {
        $page = $(this).closest('div[data-role="page"]').prev();
        $.mobile.changePage( $page, { transition: "slide", reverse: true, changeHash: false });
    });

});

Then place the previous/next buttons where ever you like, just make sure that the previous button class prevBtn and the next button class nextBtn.

For example:

<a href="#" class="ui-btn prevBtn">Previous</a>
<a href="#" class="ui-btn nextBtn">Next</a>

Upvotes: 0

Gajotres
Gajotres

Reputation: 57309

Back button :

jQuery Mobile has out of box solution for a back button on every page.

<script>
    $(document).on('mobileinit', function () {
        $.mobile.ignoreContentEnabled = true;
    });
</script> 

This code must be placed before jQuery Mobile initialization, like this:

<head>
  <title>Share QR</title>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>     
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />   
  <script>
    $(document).on('mobileinit', function () {
        $.mobile.ignoreContentEnabled = true;
    });
  </script>    
  <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>

Next button :

This one is a little bit tricky, auto generated next button will only work if you have 1 html with multiple pages solution. This will provide you with an information of a next page in line, so to create next button use this code:

$(document).on('pagebeforeshow', '[data-role="page"]', function(){       
    var nextpage = $(this).next('div[data-role="page"]');
    if (nextpage.length > 0) {    
        $.mobile.activePage.find('[data-role="header"]').append($('<a>').attr({'href':'#'+nextpage.attr('id'),'data-theme':'b'}).addClass('ui-btn-right').html('Next').button());
    }  
});  

Working example :

Here's a working example of this solution: http://jsfiddle.net/Gajotres/BnB7X/

If you have more questions feel free to ask.

Upvotes: 6

Related Questions