user1201155
user1201155

Reputation: 67

jquery index on list item issue

I'm working on getting the index of a list item contained within a UL which has the class called zoomThumbActive. I've tried just about every selector I can think of and always get -1 (not found).

I've put all the code into a fiddle:

In this example, the answer should be 3. Any idea what I'm doing wrong?

Upvotes: 0

Views: 305

Answers (2)

pete
pete

Reputation: 25091

This works:

$('#mainImgA').click(function() {
    var a = $('ul#thumblist li a.zoomThumbActive');
    var i = $('ul#thumblist li a').index(a);
    alert(i);

});

UPDATE:

$('.zoomThumbActive').index('ul#thumblist li a');

That works. I misunderstood the documentation.

Upvotes: 1

elclanrs
elclanrs

Reputation: 94121

The index needs to be called on the li element since this is the actual element descendant of ul. There's only one a inside of each li.

$('#mainImgA').click(function(){
    var idx = $('.zoomThumbActive').parent().index();
    alert(idx);
});

Upvotes: 3

Related Questions