My goal is to do something like this: https://djduncan585.github.io/noob-code-generation-problem/index2.html
I was able to accomplish this by hard-coding HTML into the index2.html file itself, then executing a JavaScript routine hard-coded using <script>
toward the bottom of the page that uses document.getElementById to access each of the individual nodes and change their content with .innerHTML; this substitutes a single randomly generate digit between 0 and 9 in any of 56 randomly selected nodes on the page forming a 14 x 4 grid using <div>
and <span>
tags.
Trouble is when I try to do 2 things: 1) Dynamically generate the page using document.write with two nested loops (I know this works properly because I copied and pasted the results of this code into index2.html to get it to function), and 2) Access the results using the same routine that I used in index2.html to change the digits, but encapsulated in an external JS document accessed in the HTML document. When I try to run this with the routines kept in a separate document (see the same address as above but subtracting the "2" from index2.html. StackExchange won't let me post more than 2 links with my current reputation level), the page renders as expected, but the looping function called later to change the contents of the nodes doesn't work.
What am I doing here that's causing the page to not animate? (See also https://github.com/djduncan585/noob-code-generation-problem.)
window.onload = () => { let changeDigits = setInterval(changeADigit(), 1000); };
<script>
function initialize() {
for (let row = 0; row < 4; row++) {
document.write('<div class="row">\n');
for (let col = 0; col < 14; col++) {
document.write(
'<span class="digit" id="digit' +
(row * 14 + col) +
'">' +
Math.floor(Math.random() * 10) +
"</span>\n"
);
}
document.write("</div>\n");
}
}
function changeADigit() {
document.getElementById(
"digit" + Math.floor(Math.random() * 56)
).innerHTML = Math.floor(Math.random() * 10);
}
initialize();
</script>