I have a DOM element elemthat has an ancestor element that has a class myClass(alternatively an attribute with some name). The problem is that I need to easily find this ancestor using jQuery . I'm currently stuck in ugly trial errors like
var ancestor = $(elem).parent().parent().parent().parent().parent().parent().parent();
If the HTML code changes, jQuery code breaks easily.
Is it possible to find an ancestor element with more elegant jQuery code?
source
share