apologies in advance if this seems super basic.
I'm confused as to why this works:
<div id="box">Hello World</div>
<button id="myButton">Click</button>
<script>
let myBox = document.getElementById('box');
let myButton = document.getElementById('myButton');
myFunction = () => {
myBox.innerHTML = 'Thanks';
}
myButton.addEventListener("click", myFunction);
</script>
But this does not:
<div class="box">Hello World</div>
<button class="myButton">Click</button>
<script>
let myBox = document.getElementsByClassName('box');
let myButton = document.getElementsByClassName('myButton');
myFunction = () => {
myBox.innerHTML = 'Thanks';
}
myButton.addEventListener("click", myFunction);
</script>
The second, which gets the DOM element by class name rather than Id generates the following error in the console:
Uncaught TypeError: myButton.addEventListener is not a function
Had searched around but cant seem to find any clear answer.
Thanks!