koen
koen

Reputation: 1057

Jquery select child of parent of anchor

I want to select and toggle() the child of the parent of the parent of an anchor.

This is an example of what I am trying to do. It doesn't work :)

<div>

    <div>
        <a href="#" onclick="$(this + ':parent:parent .inner').toggle();">hide</a>
    </div>

    <div class="inner">
        To be toggled.
    </div>

</div>

Upvotes: 3

Views: 3859

Answers (2)

Patricia
Patricia

Reputation: 7802

$(function(){
    $('a').click(function(){
        $(this).parent().parent().find('.inner').toggle();

    }

}

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630637

You could use .parent() then .siblings(), like this:

$(this).parent().siblings('.inner').toggle();

But, if you're able to make it unobtrusive that'd be a bit more maintainable, for example give your link a class, like this:

 <a href="#" class="toggler">hide</a>

Then you can bind that class (all instances of it), like this:

$(function() {
  $(".toggler").click(function() {
    $(this).parent().siblings('.inner').toggle();
  });
});

Upvotes: 6

Related Questions