For efficiency I'm tracking the click event against my nav rather than each li. However, each li has a set of span tags inside them. I need to manipulate the clicked li and get it's position (index). However, my e.target is showing my span tag rather than the li. I understand why but I'm not sure who to prevent it. My html is:
<nav id="nav"> <ul> <li><span class="num">1</span><span class="text">Contact</span></li> ... </ul> </nav> and my js is basically:
const nav = document.getElementById('nav'); nav.addEventListener('click',navClick,false); function navClick(e) { console.log(e); e.stopPropagation(); } I know I can use closest('li') to see if I'm in the li but that doesn't seem to be giving me what I want when I need to grab the li index, add/remove classes, etc.
lior use jQuery but without knowing what you mean exactly bybut that doesn't seem to be giving me what I wantit's hard to pinpoint which solution might fit best.e.target.closest('li')returns<li></li>which I don't think I can really use to extract location.