Search code examples
jqueryparent

jQuery parent()


jQuery snippet:

$(".sliders dt a").click(function(){
    $(this).parent().parent().parent().find(".triggers a").removeClass("active");
    $(this).toggleClass("active");
    $(this).parent().next("dd").slideToggle("fast");
    return false
});

HTML:

<div class="sliders">
    <div class="triggers">
       <a href="#" class="active">Hide all</a>
    </div>
    <dl>
        <dt>
            <a href="#">text</a>
        </dt>
        <dd>text</dd>
    </dl>
</div>

Three .parent() is used, to catch .triggers block. Is there any way to merge them?


jQuery 1.2.6. version is used, thats why .closest() solutions don't work.


Solution

  • You can use .closest() like this:

    $(".sliders dt a").click(function(){
        $(this).closest(".sliders").find(".triggers a").removeClass("active");
        return false
    }); 
    

    This goes up to the .sliders div then back down to find the .triggers a beneath.