I'm trying to create an event listener on clicking a checkbox (variable is 'box').
Why does box.addEventListener give an error saying 'not a function', whereas it works when I call a function that takes 'box' as a parameter?
<div class="inbox">
<div class="item">
<input type="checkbox">
<p>Item one</p>
</div>
<div class="item">
<input type="checkbox">
<p>Item two</p>
</div>
<div class="item">
<input type="checkbox">
<p>Item three</p>
</div>
<script>
The code that does work:
const box = document.querySelectorAll('input');
function clickMe(param) {
this.addEventListener('click', function () {
return console.log('hi');
});
};
clickMe(box);
The code that doesn't work:
const box = document.querySelectorAll('input');
box.addEventListener('click', function(){
return console.log('hi');
})
</script>