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

88
Visualizações
looping an array and distributing the results to three different paragraphs

I have an array of 0-3 name values. I need to loop through my array and write the values to 3 different <p> with different class names.

Not sure how to use innerHTML to assign the data values to individual divs. Plenty of info online about how to write all the data to a single div but not multiples (that i could find).

Here is the principle code...

const dataResults = ['Apple', 'Orange', 'Pear']

for (var i = 0; i < dataResults.length; i++) {
  //how to distribute these results to V1, V2 and V3???
  console.log(dataResults[i]);
}
<div class="selections">
  <p class="v1">-</p>
  <p class="v2">-</p>
  <p class="v3">-</p>
</div>

As a closing bonus I will also need to empty the array after it has completed its task.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You should dynamically build selector from your loop index:

const dataResults = ['Apple', 'Orange', 'Pear']

for (var i = 0; i < dataResults.length; i++) {
       el = document.querySelector('.v' + (i + 1));
       text = document.createTextNode(dataResults[i]);
       el.appendChild(text);

       console.log(dataResults[i]);
}
<div class="selections">
  <p class="v1">-</p>
  <p class="v2">-</p>
  <p class="v3">-</p>
</div>

about 4 years ago · Santiago Gelvez Relatório

0

const dataResults = ['Apple', 'Orange', 'Pear'];
for (var i = 0; i < dataResults.length; i++) {
    document.querySelector(".v" + (i + 1)).innerHTML = dataResults[i];
}


//Additional from the comment
document.getElementById("clearBtn").addEventListener("click", function(){
  document.querySelectorAll(".v1, .v2, .v3").forEach(function(item) {
    item.innerHTML = "";
  });
});
<div class="selections">
    <p class="v1">-</p>
    <p class="v2">-</p>
    <p class="v3">-</p>
</div>

<!--Additional from the comment -->
<button type="button" id="clearBtn">Clear</button>

about 4 years ago · Santiago Gelvez Relatório

0

One solution to the above problem is by storing the <p> tags in the list and adding the elements in it. Like:

const dataResults = ['Apple', 'Orange', 'Pear']


let d = document.getElementsByClassName('selections')[0]
let p = d.children;
let count = d.childElementCount;


for (var i = 0; i < dataResults.length; i++) {
  p[i%count].innerHTML = dataResults[i]
  console.log(dataResults[i]);
}
<div class="selections">
  <p class="v1">-</p>
  <p class="v2">-</p>
  <p class="v3">-</p>
</div>

about 4 years ago · Santiago Gelvez 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