The frenzy of choosing jQuery and the prototype

Both jQuery and Prototpye JavaScript libraries refuse to allow me to use a variable to select an element of a list item by index number, although they accept hard code.

For example, in Prototype, this works:

$$('li')[5].addClassName('active');

But this will not work no matter how I try to use the variable as a number or an integer:

$$('li')[currentPage].addClassName('active');

In jQuery, I get a similar weirdness. This will work:

jQuery('li').eq(5).addClass("active");

But this will not work again, even if the value of currentPage is 5 and its type is a number:

jQuery('li').eq(currentPage).addClass("active");

I am trying to create a pagination system in JavaScript format, and I need to set the class on the button of the active page. Elements of a list item are created dynamically depending on the number of pages I need.

+3
source
3

, , , .

jQuery('#pagination-digg li').eq(currentPage).addClass("active");
+1

, currentPage ? - :

var currentPage = 5;
jQuery('li').eq(currentPage);

. , Integer.

+5

Make sure that the currentPage variable is correctly specified in the code it accesses. Can a variable be changed somewhere else in the code before you get it? Tools like Firebug can help you add a breakpoint at runtime and see the value of your variable.

+2
source

Source: https://habr.com/ru/post/1696647/


All Articles