prestarocket
prestarocket

Reputation: 753

How to get parent selector with jquery.on('click'), base on the clicked element

I'm using jQuery.on(), and have the following code:

$('.parent').on('click', '.link',function (event) {
    console.log($(this)); // Logs '.link'
});

How can i access to the '.parent' element in which the click was triggered?

$(this) returns the '.link' element

Upvotes: 2

Views: 346

Answers (1)

A. Wolff
A. Wolff

Reputation: 74420

You can use closest():

$(this).closest('.parent');

Or use: $(event.delegateTarget) which will take into account multiple nested .parent elements.

If your element is an anchor tag, you have surely to prevent default behaviour.

Upvotes: 5

Related Questions