Finding appropriate parent with jQuery

Allain Lalonde picture Allain Lalonde · Dec 18, 2008 · Viewed 19.7k times · Source

Given a DOM element how do I find its nearest parent with a given css class?

$(".editButton").click(function() {
   (magic container selector goes here).addClass("editing");
});

I don't want to use lots or $(...).parent().parent() since I don't want to be bound to a particular dom structure.

Answer

Bill Zeller picture Bill Zeller · Dec 18, 2008

This should work

$(this).parents('.classYouWant:first').addClass("editing");