Search for all parents of elements in JavaScript

Given an element:

<header> <div> <p> <span id="elem"></span> </p> </div> </header>

Find the closest parent of this element, which is the div tag.

Given an element:

<header> <div class="www"> <p class="www"> <span id="elem"></span> </p> </div> </header>

Find the closest parent of this element, which is an element with the class www.

enru