jQuery: script remember values between times it is triggered?

Posted by Marius on Stack Overflow See other posts from Stack Overflow or by Marius
Published on 2010-04-09T16:35:31Z Indexed on 2010/04/09 16:43 UTC
Read the original article Hit count: 457

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about variables