views:

38

answers:

2

Hello there,

I am writing an ajax script in jQuery. The script gets new or previous page from a php documents that returns HTML.

If, located on page 1, I click next, the script IS able to find the next page number (page2), but when I click it AGAIN, the script again has to be able to find the next page number (page3) and at the minute it doesnt. I was wondering how I can save a variable between time a script is triggered, so that I can just + 1 to each time somebody clicks "next", and -1 when somebody clicks "previous".

This is my code:

 $('.buttonNeste').click(function(event){
  event.preventDefault();

  if (page == null || id == null ) {
   var page = parseInt($(this).closest('.paginationFullWidth').attr('id')) + 1;  
   var id = $(this).closest('.paginationFullWidth').siblings('.jtextfill').children('h1').attr('id');
  }
  else {
   var page = page + 1;  
  }

  var target = $(this);
  $.post( 'http://www.example.com/controllers/foo.php', {
   'page': (
    page
   ), 'id': 
    id
   }, function(data) {
   $(target).closest('.paginationFullWidth').siblings('.commentsContainer').html(data);
  });
 });

Thank you for your time.

Kind regards,
Marius

+1  A: 

I don't see where the closest ".paginationFullWidth"'s ID attribute is updated to 2 when you go to page 2. It seems to me that this code would always send you to page 2. When you're apparently on page 2, that ID attribute is still 1.

Patrick Karcher
+1  A: 

Assuming you declared page and id elsewhere in your code, don't re-declare them in the if and else blocks. By re-declaring them you are limiting the scope of them to those blocks.

Remove var from page and id in your if and else blocks.

Ryan Zachry
Thanks, I didnt know that! :) M
Marius