Link to home
Start Free TrialLog in
Avatar of Brad Bansner
Brad Bansner

asked on

Exclude elements with a specific class from jQuery selector with eq() filtering

Here is my selector:

var scrollDestination=-parseInt($('h2:eq('+$(this).parent('li').index()+')').parents('div.sectionwrap').position().top);

Open in new window


When the user clicks a button, this part of it generates a number:

$(this.parent('li').index()

Open in new window


This part of it finds the corresponding H2 element:

$('h2:eq(...)')

Open in new window


All I would like to do is also limit the selector so that elements with class="xyz" are ignored. So for example, get the 3rd instance of an H2 element that doesn't have class="xyz".

I tried adding :not() statements to my selector, but that doesn't seem to combine well with :eq() in the same selector, unless I'm not doing something right.

I would appreciate any advice with this.
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of skij
skij
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rainer Jeschor
Hi,
alternative the following should also work:
var curIndex = $(this).parent('li').index();
var scrollDestination=-parseInt($('h2:not(.xyz)').slice(curIndex,curIndex+1).parents('div.sectionwrap').position().top);

Open in new window


Live sample:
http://jsbin.com/qomita/211/edit?html,js,console,output

HTH
Rainer
Avatar of Brad Bansner
Brad Bansner

ASKER

Yes, that worked. Thanks! I was trying something similar, I must have just not had the syntax quite right.