I have added an on click event on a container element like so,
<li>
<div id="python-icon" class="container">
<img src="utilities/python-logo.svg"/>
<span class="ml-2"> <strong> Python </strong> </span>
</div>
</li>
document.getElementById("python-icon").addEventListener('click', e => {
debugger;
if (current_state !== e.target.id){
current_state = e.target.id;
...
}
});
The problem I am having after adding a break point, that this on click events will hit the break point if I click the child elements, i.e. the <img>
or the <span>
, however I wanted to enter this area if the on click event for the container was selected. Because the child elements enter, "e.target.id" evaluates as an empty string, breaking the logic I have entered.
Why are the child elements being hit, when I clearly in the code have used "document.getElementById" on the container, which is where I want this logic to be applied on, the container.
I have a container when an 'on_click' and all of the behaviour is attached to that container. If I click on the container, and it was registered on the child, I would like the child to propogate the event to the container, so that I can grab the "id" of the container, and not that of the child.
For example, if I wanted an extreme hack I would want to do something like this.
document.getElementById("python-icon").addEventListener('click', e => {
let comparer = e.target.id;
if (e.target.localName === "img" || e.target.localName === "span"){
comparer = e.target.parentElement.id;
}
if (current_state !== comparer){
current_state = comparer
...
}
});
but of course that feels, wrong :(