I am learning to create a calculator using Javascript. I have added my HTML elements and CSS style but I am struggling to make the eventListener work.
I have the following code (just a sample), and I have styled HTML with using grid:
HTML:
<div id="frame">
<div class="btn">1</div>
<div class="btn">2</div>
<div class="btn">3</div>
</div>
JavaScript:
document.querySelectorAll(".btn").addEventListener("click", btnClick);
function btnClick(){
alert("hi");
}
Desired outcome: Alert “Hi” whenever I click on any of the div
From everything I have learned, I thought this code should give me the desired outcome but I thought wrong. I would really appreciate if someone could point out the fault.
Many Thanks in Advance!!