Possible Duplicate:
Detecting closest or parent div
I need some help about finding elements with jquery. As topic hints, i need to find the container of given element. i already have a code like;
$('.cancel_button').bind "click", (event) ->
element = $(this.parentElement)
...
...
But it seems very ugly to me. Cancel button is in a section element. I wonder if it is possible to do something like
$(this).containerElement('section')
You may be looking for
parentorclosest:You might even want to combine them, because
closestwill return the current element if it matches. So if you had adivthat was within asectionthat was within adiv:…and you wanted to get
outerwhen an event occurred oninner, it’d be:But when the element you’re starting with doesn’t match the selector, you don’t need it.