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

235
Visualizações
Can't add text node to div class using appendChild and getElementById

trying to add text to an existing div element using a script. It should add text to the element with the id "TopBarContainer, but nothing happens when I run the script.

I tested out creating the text and just adding it to the document body whenever the script is run to make sure it is not an issue with the script call, or the creation of the text/text container and this worked fine.

I am very new to html and javascript. This seems like it should be very simple, but I've looked at the tutorial on W3, searched on stack overflow, and looked at some info on the appendChild and getElementById and wasn't able to figure out what I missed.

The relevant part of the script:

<div class="top" id= "TopBarContainer">
    <img src="webibarmax.png" id="topbar" title="The bar" alt="Superbar">
</div>

<script>
    myScript() {
    var TextContainer = document.createElement("p");
    var TextItem = document.createTextNode("textextext");
    TextContainer.appendChild(TextItem);

    document.getElementById("TopBarContainer").appendChild(TextContainer);
        

    document.body.appendChild(TextContainer);}

</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

var TextContainer = document.createElement("p");
var TextItem = document.createTextNode("textextext");
TextContainer.appendChild(TextItem);

document.getElementById("TopBarContainer").appendChild(TextContainer);
document.body.appendChild(TextContainer);
<div class="top" id="TopBarContainer">
    <img src="webibarmax.png" id="topbar" title="The bar" alt="Superbar">
</div>

As you can see from the snippet above, your code seems to be working fine.

According to the documentation you can't reference the same node twice as you are doing in your example (once in the div and another time in the body:

If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position

Could that be the issue you are having?

about 4 years ago · Juan Pablo Isaza Relatório

0

var TextContainer = document.createElement("p");
var TextItem = document.createTextNode("textextext");
TextContainer.appendChild(TextItem);

document.getElementById("TopBarContainer").appendChild(TextContainer);

You don't need append TextContainer element to the body.

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