My capture option doesn't seem to be working in the addEventListener, it gives me the same console results whether I set it to true or false.
I checked the following:
- Add
once: trueto the option object - this works, but capture doesn't - Replace
capturewithuseCapture- this also doesn't seem to work and in both cases no error/warning messages - I read online that some (older e.g. chrome version less than 55) browsers don't support newer options such as
once- I am using chrome v88 and tried opening the file with Safari too just in case it is a browser-related issue
Do you know what I am doing wrong?
function handleClick(e) {
console.log(this.classList.value);
}
const divs = document.querySelectorAll('div');
divs.forEach(div => div.addEventListener('click', handleClick, {capture: true}));
.one {
width: 200px;
height: 200px;
background-color: green;
}
.two {
width: 150px;
height: 150px;
background-color: blue;
}
.three {
width: 100px;
height: 100px;
background-color: red;
}
<div class="one">
<div class="two">
<div class="three">
</div>
</div>
</div>
Your code works just fine for me: