Both the jQuery and Prototpye JavaScript libraries refuse to allow me to use a variable to select an list item element by index number although they accept a hard coded number.
For example, in Prototype this works:
$$('li')[5].addClassName('active');
But this will not work no matter how I try to cast the variable as a number or integer:
$$('li')[currentPage].addClassName('active');
In jQuery I get similar weirdness. This will work:
jQuery('li').eq(5).addClass("active");
But this will not work again even though the value of currentPage is 5 and its type is number:
jQuery('li').eq(currentPage).addClass("active");
I'm trying to create a JavaScript pagination system and I need to set the class on the active page button. The list item elements are created dynamically depending upon the number of pages I need.
Are you certain that currentPage
is an integer? Try something like:
var currentPage = 5;
jQuery('li').eq(currentPage);
as a simple sanity check. If that works, you should try casting to Integer
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With