OK, what I'm trying to do is the following, when I click on any element on the page and I want to build up a tree of the DOM of where the element lives.
example of the html
<html>
<head>
</head>
<body>
<ul>
<li><span>Elem 1</span></li>
<li><span>Elem 2</span></li>
<li><span>Elem 3</span></li>
</ul>
</body>
</html>
This is the JS (comes from an click event and passes through the clicked element)
function getElementIdentifier(elem, domSelector, firstId) {
if(elem.tagName === 'HTML') {
return 'HTML ' + domSelector;
} else {
return getElementIdentifier(elem.parentNode, ' > ' +elem.tagName + ' ' + domSelector, firstId);
}
}
which works to an extent but when it comes to the fact that I've clicked on the third item in the list it only retrieves the following:
HTML > BODY > UL > LI > SPAN
But I want to retrieve the 3rd as this is the one I clicked. I've got a codepen I did to show.
http://codepen.io/tom-maton/pen/FljpL/
I'm not wanting to use jQuery - just looking to use raw js.