Search code examples
jqueryfindparent

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.


Solution

  • 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');