What is the best way to find out if a given element (targetElement) is inside of an element with a specific type like for example an anchor in pure JavaScript?
(html)
<a href="/"> <div class="AnElementInBetween"> <div class="anotherElementInBetween"> <div class="targetElement" onclick="handleClick()"> Am I inside an anchor element? </div> </div> </div> </a> (js)
function handleClick(event){ // event.target Am I inside an anchor? } Since some people have seen a duplicate in this question, just to make this clear. The questions looks for a solution to check if the element is surrounded by a specific element type not a specific element itself.
.contains(). Also your comment seems to be a duplicate since you postet it twice.