I'm doing the 10 days of JS on Hackerrank and I'm stuck on day 9 where you have to make a button and on each press it increments the number on the button by one. I've written this code but it doesn't seem to do anything. The button looks fine but the onclick doesn't work. I also want to do this without writing something along the lines of onclick="increment()" in the html code because I can already do that. To test my code I went into w3schools.com and found a random test and replaced the code with my own:
var btn = document.getElementById('btn');
var i = 0;
btn.onclick = function() {
i++;
btn.innerHTML = i;
};
.btnClass {
width: 96px;
height: 48px;
font-size: 24px;
background: #4FFF8F;
}
<h2>The Button Element</h2>
<button id="btn" class="btnClass">0</button>