I'm doing a small application just to learn ES6 stuff and I've found a problem with addEventListener
and parent/child events.
I have a menu consisting on several divs that have an image (an avatar) and some text. Each div has an data-id attribute to get the clicked elements id and I have put an addEventListener
on each row:
<div class="row" data-id="1">
<img src="avatar" />
Lorem ipsum
</div>
...
And the js:
const rows = document.querySelectorAll('.row');
rows.forEach(row => row.addEventListener('click',selectRow));
function selectRow(e){
var row = e.target;
alert(row.dataset.id);
}
The problem comes when I click on the avatar. It fires the click event, but the target is the img element, not the div, so I can't get the data-id attribute.
I have tried many approaches (like this one) but it prevents firing the event when clicking on the avatar, so not on a solution as the avatar is part of the row and it may get clicked :S
I have set a jsfiddle to show my problem. Any help would be appreciated :)