0

The result of document.getElementById('boxed').innerHTML = numList.join(", "); is a blank div with console error: TypeError: document.getElementById(...) is null. Heres the whole HTML:

    <!DOCTYPE html>
<html lang="en">

<head>
    <title>Super Lotto</title>
    <meta charset="utf-8">
    <link href="https://fonts.googleapis.com/css?family=Libre+Franklin" rel="stylesheet">
    <link href="lotto-styles.css" rel="stylesheet">
    <script>
        do {
            var high = prompt('What\'s the highest number you want to generate','');
            high = parseInt(high, 10);
        } while (isNaN(high));

        do {
            var nums = prompt('How many numbers do you want to generate','');
            nums = parseInt(nums, 10);
        } while (isNaN(nums));


        function rand(highestNum) {
            var randomNumber =
            Math.floor(Math.random() * highestNum) + 1;
            return randomNumber;
        }

        var numList = [];

        for (var i = 0; i < nums; i++) {
            // Go through this loop quantity of times
            numList.unshift(rand(high)); 
            // add new number to end of array
        };

        document.getElementById('boxed').innerHTML = numList.join(", ");
    </script>
</head>

<body>
    <div id="container">
        <header>
            <h1>Lucky Numbers</h1>
        </header>

        <main>
            <div id="boxed"></div>
            <p>Good luck!</p>
        </main>
    </div> <!-- Closing container -->
</body>

</html>
Don
  • 3
  • 2
  • 1
    As far as I remember, the script tag with no extra params will be read and execute immediately the code, therefore your `document.getElementById('boxd')` won't find the tag because it hasn't been read yet. You should add the *defer* attribute to your script tag or add the script at the bottom within the body tag. Find more information in here: https://javascript.info/script-async-defer – Diego Molina Jun 03 '20 at 01:45
  • 1
    Thank you! Solved by having the script fire after body was loaded. I have been working with Jquery's `$(document).ready()` most of the semester and overlooked that :) – Don Jun 03 '20 at 02:05

0 Answers0