I'm trying to do event delegation in vanilla JS. I have a button inside a container like this
<div id="quiz">
<button id="game-again" class="game-again">
<span class="icon-spinner icon"></span>
<span>Go again</span>
</button>
</div>
And following David Walsh's nice instructions I'm adding an event handler to an ancestor of the button like so:
this.container.addEventListener('click', function(e){
if (e.target && e.target.id == 'game-again') {
e.stopPropagation();
self.publish('primo:evento');
}
});
Where this.container
is the #quiz element. This works half the time, but the rest of the time the target of the click event is one of the spans inside the button, so my event handler isn't called. What's the best way to deal with this situation?
.matches
as long as you get the unprefixed version and usematches.call(e.target,"#game-again,#game-again *")
- see my answer for more details. – Antenatal<button data-action="tasks.remove">X</button>
. And you can pass custom data to each of your action handlers taken from thedata-action-params
HTML5 data attribute. – Safford