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

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

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 Relatório

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