Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why isn't my .inner HTML working?

+2 votes
asked Feb 14, 2023 by JuSt HoTMaiL CurSeS (330 points)
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style>
            my animations
  </style>
</head>
<body>
  <script>
        var points = 0;
        while (0==0) {
            points++;
            document.getElementById('points').innerHTML = points;
        }
        function gameOver() {
            this.style.background = 'black';
            alert('Game Over!');
            alert('You won ' + points + ' points, congratulations!');
            close();
        }

  </script>
  <h1 id="scoreContainer">Score:</h1>
  <h1 id="points">0</h1>
  <div id="divide_one" class="divLeft-Right" onmouseover="gameOver();"></div>
  <div id="divide_two" class="divLeft-Right" onmouseover="gameOver();"></div>
  <div id="divide_three" class="divLeft-Right" onmouseover="gameOver();"></div>
  <div id="divide_four" class="divUp-Down" onmouseover="gameOver();"></div>
  <div id="divide_eight" class="divUDLR" onmouseover="gameOver();"></div>
  <div id="divide_nine" class="divUDLR" onmouseover="gameOver();"></div>
  <div id="divide_ten" class="divUDLR" onmouseover="gameOver();"></div>
  <div id="divide_eleven" class="divUDLR" onmouseover="gameOver();"></div>
</body>
</html>

Thank you in advance!

2 Answers

0 votes
answered Mar 14, 2023 by dadsdy (220 points)
While I'm not entirely sure why that wouldn't work (I think that the getElements return something that changes into the element, or it tries to change the function or somethig), if you assign the element to a variable, and change it, then it will work. Something like this:
let pointText=document.getElementById('points');
pointText.innerHTML = points;
0 votes
answered Mar 14, 2023 by Henrique Adriano (140 points)

Sim, existem alguns erros no código HTML fornecido. Abaixo está uma lista dos problemas que foram encontrados:

  1. A classe "divLeft-Right" foi escrita com um espaço no meio em algumas das tags div.
  2. A tag "h1" com o ID "points" não tem nenhum conteúdo dentro dela.
  3. A função "fechar()" chamada na função "gameOver()" não está definida no código fornecido.
  4. Há um espaço extra no fechamento da tag "body".

Aqui está o código corrigido com esses problemas resolvidos:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style>
    /* Adicione animações aqui */
  </style>
</head>
<body>
  <h1 id="points">0</h1>
  <div id="divide_one" class="divLeft-Right" onmouseover="gameOver();"></div>
  <div id="divide_two" class="divLeft-Right" onmouseover="gameOver();"></div>
  <div id="divide_three" class="divLeft-Right" onmouseover="gameOver();"></div>
  <div id="divide_four" class="divUp-Down" onmouseover="gameOver();"></div>
  <div id="divide_eight" class="divUDLR" onmouseover="gameOver();"></div>
  <div id="divide_nine" class="divUDLR" onmouseover="gameOver();"></div>
  <div id="divide_ten" class="divUDLR" onmouseover="gameOver();"></div>
  <div id="divide_eleven" class="divUDLR" onmouseover="gameOver();"></div>

  <script>
    var pontos = 0;
    function gameOver() {
      this.style.background = 'black';
      alert('Fim do jogo!');
      alert('Você ganhou ' + pontos + ' pontos, parabéns!');
      // fechar();
    }
    while (true) {
      pontos++;
      document.getElementById('points').innerHTML = pontos;
    }
  </script>
</body>
</html>


Observe que a declaração "while (true)" dentro da função "gameOver()" criará um loop infinito que impedirá que o resto do código seja executado. Portanto, é importante removê-lo ou alterá-lo para uma condição de saída apropriada. Também é importante lembrar que, em vez de usar o alerta para exibir o número de pontos, pode ser melhor exibir o valor em uma tag HTML na página.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...