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

123
Views
Having issues creating a for loop for a list created in JS

I'm trying to for loop the H1 object through a list 10 times. I'm not sure where I went wrong any help would be appreciated.

var headOne = document.createElement("H1");
headOne.textContent = "Hello World";
document.body.appendChild(headOne);

var newOrderedList = document.createElement('OL');
newOrderedList.setAttribute("id", "OLJS");
document.body.appendChild(newOrderedList);

var helloWorld = document.getElementById("OLJS");

for (var i = 0; headOne < 10; i++){
  var listItems = document.createElement("li");
  listItems.innerHTML = headOne[i];
  helloWorld.append(listItems);
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you want to loop 10 times then do:

for (let i = 0; i < 10; i++) {
  // Do something
}

And in your case if you are trying to access each letter of headOne element and append it to the helloWorld list then you can do the following:

for (let i = 0; i < headOne.textContent.length; i++) {
  let listItems = document.createElement('li')
  listItems.textContent = headOne.textContent[i]
  helloWorld.append(listItems)
}

You might also want to read more about Loops and iteration

about 4 years ago · Juan Pablo Isaza Report

0

var headOne = document.createElement("H1");
headOne.textContent = "Hello World";
document.body.appendChild(headOne);

var newOrderedList = document.createElement('OL');
newOrderedList.setAttribute("id", "OLJS");
document.body.appendChild(newOrderedList);

//var helloWorld = document.getElementById("OLJS");

for (var i = 0; i < 10; i++) {
  var listItems = document.createElement("li");
  listItems.innerHTML = "order list item " + (i + 1);
  newOrderedList.append(listItems);
}

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!