Scenario: I have an unordered list of list items's. In each list item is a span and in each span is an img tag. So my html structure looks like this.
<ul class="slider-controls">
<li data-preview1="./images/1.1.jpeg" data-preview2="./images/1.2.jpeg"><span><img src="./images/color1.jpeg"></img></span></li>
<li data-preview1="./images/2.1.jpeg" data-preview2="./images/2.2.jpeg"><span><img src="./images/color2.jpeg"></img></span></li>
<li data-preview1="./images/3.1.jpeg" data-preview2="./images/3.2.jpeg"><span><img src="./images/color3.jpeg"></img></span></li>
</ul>
The img tags are just tiny square colour swatches, and the spans are styled into circles so what you have is basically a colour picker of three coloured dots for the user to click on.
When a user clicks on the li, I'm using javascript to grab the data-previews so that I can use that information to change a picture. This works perfectly if the user clicks slightly outside the circle. However, if they click inside the circle, they end up click on the img tag. But the data I need is two parent nodes up in the li tag!
So I wrote a recursive function to address this.
function findUpTag(el, tag) {
console.log(el.tagName, tag);
if (el.tagName === tag) {
console.log("success!", el);
return el;
} else {
findUpTag(el.parentNode, tag);
}
}
I use it like so findUpTag(el, "LI");
I know my recursive search is working because I always get the console.log("success") output when the current element === "LI".
However, my return value when I click on the image tag is always undefined! Even when my method finds the LI!
What am I doing wrong?