Walrus
Walrus

Reputation: 20484

jQuery opposite of find()

The following element:

<div class="hslide"></div>

contains a series of other elements and is repeated several times on a page.

Inside the content are triggers that interact with the element. At the moment the triggers cause the same thing to happen with all hslide elements.

ie. var current = $('.hslide .slide.current');

Is there a way of selecting the correct .hslide because the trigger contained within it is contained within it. I could use parent() but the trigger is not always only one layer down.

Upvotes: 1

Views: 975

Answers (2)

Jai
Jai

Reputation: 74738

As you mentioned to select the correct parent, so you should use .closest() to traverse up to the parent element:

var current = $('.slide.current').closest('.hslide');

Upvotes: 3

SLaks
SLaks

Reputation: 887897

You're looking for .closest(selector), which returns the innermost matching parent element.

Upvotes: 1

Related Questions