Display content that was initially hidden onload

559 views Asked by At

resultTable is initially hidden. When play button is clicked, the game will run and it will display the finalscore inside the hidden content. I need a function to display the finalscore. Currently the showResult function I have is not working (obviously)

I deleted unnecessary contents because the whole thing is a little big and messy. Hope the code still makes sense.

<body onload="loadGame()">
    <div>
        <button onclick="playButton()" id="play">Play</button>
    </div>
    <div id="resultTable" >
        <span id="result"></p>
    </div>

    <script>
        function loadGame(){
            var hideResult = document.getElementById("resultTable");
            hideResult.style.display = "none";
        }
        function playButton(){
            playGame();
            showResult();
        }
        function playGame(){
            /*Some code here*/
            document.getElementbyId("result").innerHTML = "finalscore";
        }
        function showResult(){
            var show = document.getElementById("resultTable"); //fixed.
            show.style.display = "block";
        }
    </script>
</body>        
1

There are 1 answers

0
Ash On

Change the way you are finding the element to set its display as block or none. You are using the "getElementsByClassName" method, but there is no element with such classname in the DOM.

Moreover, the "getElementsByClassName" will return you an array of all the elements, if found, in the DOM and you have to loop through the array to access it.

 function showResult(){
            document.getElementById("resultTable").style.display = "block";
        }