Let's assume that you have the following HTML structure:
HTML<div class = 'Family'> <div class = 'Dad' id = 'Dad'> <div class = 'Child'>I'm a child</div> <div class = 'Subchild'>I'm a subchild</div> <div class = 'Child'>I'm a child</div> </div> <div class = 'Uncle'>I'm the uncle</div> </div>
If you want to know if the click has been done inside the parent element:
javascriptconst handleClick = (e) => { let parent = document.getElementById('Dad'); if(parent.contains(e.target)) alert('Dad sponsors the click'); } window.addEventListener('click', handleClick);
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.