niko craft
niko craft

Reputation: 2977

jQuery get parent with specific class

I am listening to clicks on an element with a specific class. When I click inside this element I want to get this element, not the children I clicked on. How do I avoid getting children back when I call

event.target

Here is fiddle I created to demonstrate the problem https://jsfiddle.net/yxvfgkcs/

Since I get children back they get CSS applied to them instead of the parent.

How do I figure out I have clicked on a child of element and get the element with class selectable back?

Upvotes: 0

Views: 42

Answers (1)

Jaqen H'ghar
Jaqen H'ghar

Reputation: 16804

Since you're already using jQuery you can use .closest():

var clickedField = $(e.target).closest(".selectable");

See updated JSFiddle

Upvotes: 1

Related Questions