Here is my code.
JavaScript
x
7
1
<button id ="id"><span>fire!</span></button>
2
<script>
3
document.getElementById('id').addEventListener('click',e=>{
4
console.log(e.target)
5
})
6
</script>
7
then we can find simple button in DOM. Once I click button, script returns
JavaScript
1
2
1
<span>fire<span>
2
or
JavaScript
1
2
1
<button id="id"> </button>
2
.
How can I add event listener to just button element?
Advertisement
Answer
This is as one of the options… You can use the currentTarget
.
In your case, it will be like this:
JavaScript
1
3
1
document.getElementById('id').addEventListener('click', e=> {
2
console.log(e.currentTarget);
3
});
JavaScript
1
1
1
<button id ="id"><span>fire!</span></button>