Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
why isnt my .innerHTML working, javascript game

i swear i can do this an it must just be a machine error. I have the same thing working in over programs but within a method. I was simply coping someone elses working code. I cant update the score from 0 to 1. Can anyone take a look? must be the tiniest logic error ever or something. Yes im calling my css and js. Thanks in advance

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clicker</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <script src="game.js"></script>

    <h1>CLICKER</h1>
    <p id="score">Score: 0</p>
    <img id="algae" src="/images/algaepng.png" alt="Image">
    
</body>
</html>

FROM GAME.JS

let score = 0;
score = score +1;
document.getElementById('score').innerHTML = "Score: " + score;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your javascript code is executed before the HTML code
Solution 1: Declare the script in the head with the word defer

let score = 0;
score = score + 1;
document.getElementById('score').innerHTML = "Score: " + score;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Clicker</title>
    <link rel="stylesheet" href="styles.css" />
    <script src="game.js" defer></script>
  </head>
  <body>
    <h1>CLICKER</h1>
    <p id="score">Score: 0</p>
  </body>
</html>

Solution 2: declare your javascript at the end of the body

let score = 0;
score = score + 1;
document.getElementById('score').innerHTML = "Score: " + score;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Clicker</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>CLICKER</h1>
    <p id="score">Score: 0</p>
    <script src="game.js"></script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda