1

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?

1 Answer 1

2

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());
Sign up to request clarification or add additional context in comments.

3 Comments

Thx for the answer, but I'm still only getting '-1' all the time. Did it work for you in the fiddle?
According to your simple example it is working . look here. jsbin.com/wuvalo/1/edit?html,js,output. you can't show html and ask about that html and supply another jsfiddle.
Hmm.. You're right. Do you mind having a look at my updated fiddle and see what I'm doing wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.