I was following along an online tutorial and want to show the alert below as soon as one of the numbers 0-9 is clicked.
For whatever reason, it works when I copy and paste the code in here, however I tried the very same code in different browsers (with no ad blockers etc.) and no pop up window was shown. Maybe someone knows why. Thanks for reading or even helping a beginner out!
let operator = document.getElementsByClassName('num');
for(let i = 0; i<operator.length; i++) {
operator[i].addEventListener('click', function () {
alert('The operator clicked:'+this.id);
})
}
body {
background-color: lightblue;
height: 100%;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
grid-gap: 6 px;
border: 1px black solid;
width: 80%;
}
.spec, .num {
border-radius: 50%;
width: 25 %;
font-weight: bold;
cursor: pointer;
font-size: 16px;
}
.spec:active, .num:active {
font-size: 13px;
}
.inp {
width: 80%;
height: 30px;
margin-bottom: 20px;
border-radius: 5px;
}
button:nth-child(4n) {
background-color: lightgray;
}
<!DOCTYPE html>
<html>
<head>
<title>Calculator on 1308</title>
<link rel="stylesheet" type="text/css" href="/Users/fab/Downloads/1308/style.css">
<script src="/Users/fab/Downloads/1308/script.js"></script>
</head>
<body>
<input type="text" class="inp">
<div class="container">
<button class="spec">C</button>
<button class="spec">CE</button>
<button class="spec">%</button>
<button class="spec">/</button>
<button class="num" id="7">7</button>
<button class="num" id="8">8</button>
<button class="num" id="9">9</button>
<button class="spec">x</button>
<button class="num" id="4">4</button>
<button class="num" id="5">5</button>
<button class="num" id="6">6</button>
<button class="spec">-</button>
<button class="num" id="1">1</button>
<button class="num" id="2">2</button>
<button class="num" id="3">3</button>
<button class="spec">+</button>
<button class="num" id="last_line">0</button>
<button class="spec" id="last_line">=</button>
</div>
</body>
</html>