<p>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. </p>

<p>For example, in Prototype this works:</p>

<pre><code>$$('li')[5].addClassName('active');
</code></pre>

<p>But this will not work no matter how I try to cast the variable as a number or integer:</p>

<pre><code>$$('li')[currentPage].addClassName('active');
</code></pre>

<p>In jQuery I get similar weirdness. This will work:</p>

<pre><code>jQuery('li').eq(5).addClass("active");
</code></pre>

<p>But this will not work again even though the value of currentPage is 5 and its type is number:</p>

<pre><code>jQuery('li').eq(currentPage).addClass("active");
</code></pre>

<p>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.</p>

[{'answer_id': 40599, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 3, 'selected': False, 'text': "<p>Are you certain that <code>currentPage</code> is an integer? Try something like:</p>\n\n<pre><code>var currentPage = 5;\njQuery('li').eq(currentPage);\n</code></pre>\n\n<p>as a simple sanity check. If that works, you should try casting to <code>Integer</code>.</p>\n"}, {'answer_id': 40627, 'author': 'Dan Cramer', 'author_id': 3274, 'author_profile': 'https://Stackoverflow.com/users/3274', 'pm_score': 2, 'selected': False, 'text': '<p>Make sure that the currentPage variable is correctly scoped in the code where it is being accessed. Could the variable be changed somewhere else in the code before you are accessing it? Tools like <a href="https://addons.mozilla.org/firefox/addon/1843" rel="nofollow noreferrer">Firebug</a> can help you to add a breakpoint at the point of execution and see the value of your variable.</p>\n'}, {'answer_id': 40800, 'author': 'user2601', 'author_id': 2601, 'author_profile': 'https://Stackoverflow.com/users/2601', 'pm_score': 3, 'selected': True, 'text': '<p>It looks like I just needed to be more specific in my element selector although it is weird that a hard coded number would work.</p>\n\n<pre><code>jQuery(\'#pagination-digg li\').eq(currentPage).addClass("active");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/40590', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4320/']