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

171
Visualizações
Script cannot find attributes of an appended html element

It outputs undefined for both. It should output lpoop and /poop. No errors are given

function add() {
  console.log("add")
  var l = document.createElement("img")
  l.setAttribute("src", "/poop")
  l.src = "/poop"
  l.setAttribute("class", "l")
  l.className = "lpoop"
  document.getElementById("b").appendChild(l)
}

function find() {
  console.log("find");
  const elm = document.getElementById("b").firstChild;
  console.log(elm.className);
  console.log(elm.src);
}
<button onclick="add()">
  hi
</button>
<button onclick="find()">
find
</button>

<div id="b">

</div>

If there is any more detail you want pls comment.

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

0

function add() {
  console.log("add")
  var l = document.createElement("img")
  l.setAttribute("src", "/poop")
  l.setAttribute("class", "l")
  document.getElementById("b").appendChild(l)
}

function find() {
  console.log("find");
  const elm = document.getElementById("b").firstChild;
  console.log(elm.getAttribute("src"));
  console.log(elm.getAttribute("class"));
}
<button onclick="add()">
  hi
</button>
<button onclick="find()">
find
</button>

<div id="b"></div>

When you use elem.attr it means that you are getting the JS Attribute of the element, not the HTML attribute.

The .getAttribute() function will get the HTML attribute.

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