Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and Prototype Selector Madness

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.

like image 438
rsrobbins Avatar asked Jan 24 '23 03:01

rsrobbins


1 Answers

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.

like image 131
John Millikin Avatar answered Jan 30 '23 11:01

John Millikin