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

176
Visualizações
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 Respostas
Responde à pergunta

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 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