Working
inside a button Not working
What you actually want to do here is have a click event on the button - always the button, as that is the native HTML element and has built in accessibility for all.
The reason your button click won't fire if you click on something wrapped in a span (or or etc) inside a button is because you are listening for a click on event.target (what's actually clicked) and not on event.currentTarget.
If you want to listen to the button click itself (a click anywhere inside the button, including the span) you would use something like this:
HTML:
<button type="button" class="jsAlertOnClick">
Inside a button
<span onClick="alert('Span Clicked, and...');">I'm in a span</span>
</button>
Javascript:
const myButton = document.querySelector('.jsAlertOnClick');
function handleBtnClick(event) {
const btn = event.currentTarget;
window.alert(btn + 'has been clicked!');
}
myButton.addEventListener('click', handleBtnClick);
If you click on the span, that alert will fire first, followed by the button one. If you click elsewhere in the button, only that alert will fire.