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

220
Visualizações
How to append elements of array to DOM?

I am having trouble with adding elements to my HTML. I think my issue lies in my understanding of HTML but I'm not really sure. Here is my Javascript:

<div class="console">
    <div id="console_data">
        <p class="console_text"> </p>
    </div>
</div>

Let me explain what this is doing, or at least what I'm trying to do. The console class creates just a black rectangle, and it's gonna have some white text in it, which is what console_text is for. Here is my javascript to append things to console_data:

var consoleData = ["Hello World"]
var consoleInfo = document.getElementById('console_data')

for (var i = 0; i < consoleData; i++) {
    var anchor = document.createElement('a')
    var value = consoleData[i]
    anchor.href = value
    consoleInfo.appendChild(anchor)
}

I was under the impression this would append the text Hello World inside of the black box I created, but it's just showing the empty black box. Is my console_data tag even necessary? Where should I be trying to append my data to?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should be checking whether i is smaller than consoleData's length, since you are iterating through it:

var consoleData = ["Hello World"]
var consoleInfo = document.getElementById('console_data')

for (var i = 0; i < consoleData.length; i++) {
    var anchor = document.createElement('a')
    var value = consoleData[i]
    anchor.href = value
    anchor.innerHTML = value
    consoleInfo.appendChild(anchor)
}
<div class="console">
    <div id="console_data">
        <p class="console_text"> </p>
    </div>
</div>

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