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

171
Vistas
The difference between element.innerHTML and document.writeln?

I'm a newbie on JavaScript. I can't understand why if I write

document.getElementById('btn').addEventListener('click', function(){
  for (i=0; i<=10; i++){
    let writeNumber = document.getElementById('paragraph');

    writeNumber.innerHTML = i;
    
  }
})

I read only the last number, in this case '10', and if i write

document.getElementById('btn').addEventListener('click', function(){
  for (i=0; i<=10; i++){
    document.writeln(i);
  }
})

I can read all the numbers required. Thanks and sorry if this seems like a trivial question

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

document.getElementById('btn').addEventListener('click', function(){
  for (i=0; i<=10; i++){
    let writeNumber = document.getElementById('paragraph');

    writeNumber.innerHTML = i;
    
  }
})

The above code will replace the content of the paragraph element with 10.

You can modify it to view all the numbers as below

document.getElementById('btn').addEventListener('click', function(){
  for (i=0; i<=10; i++){
    let writeNumber = document.getElementById('paragraph');

    writeNumber.innerHTML += `${i} `;
    
  }
})
document.getElementById('btn').addEventListener('click', function(){
  for (i=0; i<=10; i++){
    document.writeln(i);
  }
})

The code appends to the document with a new line character everytime it is called.

document.writeln(i) -> This only works on the main document and everything will be removed from the DOM. (So don't use this)

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