Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!