simPod
simPod

Reputation: 13466

Delegate jquery - get parent container

I want to select all p elements in .section container, but just in the one that contains the a.open link that has been clicked

$('.section').delegate('a.open', "click", function(){
    $(this).parent().filter('p').show(); //I want to select all `p` elements in .section container, but just in th one that containts the a.open link that has been clicked
})

Thanks

Upvotes: 2

Views: 3832

Answers (3)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You could pass the delegateTarget as context (delegateTarget is a property of the event object which is passed to handler function and is the DOM element which "Handles" the delegation

$('.section').delegate('a.open', "click", function(e){
    $('p', e.delegateTarget).show()
    //This means show all <p> elements in the context defined by e.delegateTarget
})

look at this fiddle http://jsfiddle.net/jWYKv/

Upvotes: 7

Lee
Lee

Reputation: 10603

$(this).parents('th:first').find('p').show();

What about that ?

Edit: misinterpreted your explaination of the problem

$(this).parents('.section:first').find('p').show();

Upvotes: 1

Lasse Espeholt
Lasse Espeholt

Reputation: 17782

Try this:

Closest (goes up the tree and stops at the first .section), Find (finds all descendants filtered by the given selector)

$('.section').delegate('a.open', "click", function(){
    $(this).closest('.section').find('p').show();
})

Upvotes: 2

Related Questions