I am trying to use JavaScript to change the HTML contents of the webpage. I have a set of numbers, 0 to 9, and I am trying to use JavaScript to change it from 9 to 0.
The HTML and JS are
function flip(amount) {
var x = 0;
for (i = amount; x < amount; x++) {
setTimeout(() => {
document.getElementById(String(x)).innerText = String(amount - x - 1);
}, 2000);
console.log(x);
}
}
<span id="0">0</span>
<span id="1">1</span>
<span id="2">2</span>
<span id="3">3</span>
<span id="4">4</span>
<span id="5">5</span>
<span id="6">6</span>
<span id="7">7</span>
<span id="8">8</span>
<span id="9">9</span>
<button id="b1" onclick="flip(10);">Change</button>
In the console, x was logged all ten times correctly. In the console, it said
Uncaught TypeError: Cannot set property 'innerText' of null at change.html:25
(change.html is the name of the document)
I don't know what that's supposed to mean or how I can fix it.