tags:

views:

138

answers:

3

I want to store the current page number in a jquery variable, but I am not sure if it is possible, so I am storing it in an input field. Am I doing it the most efficient way?

<input type='hidden' id='current_page' /> 

$('#current_page').val(0);  

if page is changed,

function (newpage){
$('#current_page').val(page_num);
}
+1  A: 

With JQuery you can store data in any DOM element, input or not

$('#current_page').data('page', 0); // save value
$('#current_page').data('page'); // return value

http://api.jquery.com/data/

As I said, you don't have to add special html element for that, any one existing will work.

Nikita Rybak
so I would need to create a div or input to store the data?
Scarface
@Scarface Any existing DOM element will work. (even 'body' tag)
Nikita Rybak
Is it good practice to store many different variables in body tag?
Scarface
@Scarface It's probably better to store table-related vars in the table itself (e.g., root element of it). But body will work too.
Nikita Rybak
+1  A: 

You can use jQuery.data() for this -as mentioned by Nikita.

But if you'd like to support the JavaScript-disabled clients as well, then I'd stick to a hidden input element. This way you can still control the pagination from the server side on. The hidden input value can then be used to inform the server side what page the client is currently sitting in.

BalusC
I was told before that <div id="currentpage" page="0"></div> is bad practice, isn't doing $('#current_page').data('page', 0); the same thing?
Scarface
No, certainly not. It's stored as a [HTML data attribute](http://ejohn.org/blog/html-5-data-attributes/). You aren't abusing a semantic element for other purposes then. You are just taking benefit of the attribute space. See also [this w3 spec](http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data-with-the-data-attributes).
BalusC
both great references, which leaves just one more question for me, is it better to use jquery data function to store information in dom or use a data attribute and update it and extract information from it?
Scarface
Doesn't matter. But if you'd like to prefill it with server-side generated data beforehand, then I'd just use the data attribute directly instead of afterwards filling using jQuery during document load or so.
BalusC
thanks again, appreciate it
Scarface
A: 

The other 2 answers are correct, and you can also store it in a javascript variable if you want. Just use a closure. Something like this (but I'm not running this code ... it's conceptual only).

$(document).ready(function() {
   var pageNumber = 0;

   // This func can access "pageNumber" even
   //  though the var was declared in outer
   //  scope.
   function goToNewPage(newPageNumber) {
      pageNumber = newPageNumber;
   }

   // This func can access "pageNumber" even
   //  though the var was declared in outer
   //  scope.
   function getCurrentPageNumber() {
      return pageNumber;
   }

}
Charlie Flowers