Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

getting index based on class

I need to get the index of the element with class .active but seem unable to. Here is a fiddle demonstrating it (I've commented the line I'm interested in). If you press the pager-numbers it should display the correct index of the one marked red.

I have tried the following:

console.log($('.clickable').index('.active'));
console.log($('.pager').index('.active'));
console.log($('div.pager span.clickable').index('.active'));
console.log($('.active').index());
console.log($('.active').closest('.pager').find('.clickable').index('.active'));
console.log($('.clickable').index($('.clickable').filter('.active')));
console.log($('span.clickable').index('.active'));
console.log($('span.active').index());
console.log($('div.pager span.active').index());

This is how it appears in the HTML:

<div class="pager">
    <span class="page-number clickable">...</span>
    <span class="page-number clickable">2</span>
    <span class="page-number clickable active">3</span>
    <span class="page-number clickable">4</span>
    <span class="page-number clickable">...</span>
</div>

At this moment I'm just spamming different variations I've found on google/SO.. why won't anything work?

Upvotes: 1

Views: 31

Answers (1)

Royi Namir
Royi Namir

Reputation: 148524

I need to get the index of the element with class .active

Instead of

console.log($('.clickable.active').index('.active'));

do :

console.log($('.clickable.active').index());

Upvotes: 2

Related Questions