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

260
Visualizações
What is wrong with my JavaScript Code using the innerHTML method?

I'm trying to change the text in an h2 element with the simplest code but don't get what I'm doing wrong :

html

<h2 id="tries">Number of tries : 0</h2>

javascript

document.getElementById("tries").innerHTML = 'new text';
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is nothing wrong wiht that. You asked JS to replace the innerHTML, and JS done that.

If you want to change only the value after the ":" then here is an example where I placed a span into the the p and I change the innerHTML of this span.

function changeText(value) {
  //this is the point
  document.getElementById("tries-value").innerHTML = value;
}

const input = document.querySelector("input");
input.addEventListener("change", (e) => changeText(e.target.value));

changeText(input.value)
<h2 id="tries">Number of tries : <span id="tries-value">0</span></h2>
<label for="input-number">Change the input:</label>
<input id="input-number" value="10" type="number" />

about 4 years ago · Juan Pablo Isaza Relatório

0

I just guess you did that and as you can see, it will fail

<!doctype html>
<html>
<head>
    <script>
document.getElementById("tries").innerHTML = 'new text';
    </script>
</head>
<body>
    <h2 id="tries">Number of tries : 0</h2>
</body>
</html>

You can first do DOM Operations if the DOM is actually loaded, so you just listen to the window.load event and it will work

<!doctype html>
<html>
<head>
    <script>
window.addEventListener('load', function () {
    document.getElementById("tries").innerHTML = 'new text';
});
    </script>
</head>
<body>
    <h2 id="tries">Number of tries : 0</h2>
</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