Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!