Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda