In jQuery 1.6, you can use the following code in the general case, for example. targetElt and parentElt can be either DOM elements or jQuery wrapped objects, as well as selectors:
$(targetElt).closest(parentElt).length > 0
Some of the other answers require you to reference elements by their identifiers, which is not useful if all you have is a DOM element without an identifier. In addition, if you want to make sure that targetElt is a strict descendant of parentElt (in other words, you do not want to consider parentElt as your own descendant), be sure to add the check targetElt != parentElt before calling .closest() , or use .parents().find() as suggested by Jonathan Sampson.
Nathaniel May 4 '11 at 23:38 2011-05-04 23:38
source share