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

103
Views
Adding UL using DOM Manipulation Not Working. Help is appreciated

<!DOCTYPE html>
<html>
  <head>
    <title>Learning About Nodes</title>
  </head>
  <body>
    <ul>
      <li>Hammerhead</li>
      <li>Tiger</li>
      <li>Great White</li>
    </ul>
    <button id="btn">Just do it</button>
  </body>
</html>

btnElement = document.getElementById("#btn");
    if (btnElement) {
      btnElement.addEventlistener("click", justdoitFunction);
    }

    function justdoitFunction() {
      const ul2 = document.createElement("ul");
      document.body.appendChild(ul);
      const listElement = document.createElement("li");
      listElement.textContent = "This is done by Panda.";
      
      ul2.appendChild(listElement);
    }

I am trying to add a text content using the just do it button. I don't see what the mistake is. Help is greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have found some syntax errors, so i fixed it here:

const btnElement = document.getElementById("btn");

function justdoitFunction() {
  const ul = document.createElement("ul");
  document.body.appendChild(ul);
  const listElement = document.createElement("li");
  listElement.textContent = "This is done by Panda.";

  ul.appendChild(listElement);
}


if (btnElement) {
  btnElement.addEventListener("click", justdoitFunction);
}

But I can see that you are trying to insert an item into a ul list. Instead of creating a new whole list each time you click the button, try to select the existing list and add the item there:

<!DOCTYPE html>
<html>
  <head>
    <title>Learning About Nodes</title>
  </head>
  <body>
    <ul id="list">
      <li>Hammerhead</li>
      <li>Tiger</li>
      <li>Great White</li>
    </ul>
    <button id="btn">Just do it</button>
  </body>
</html>
const btnElement = document.getElementById("btn");

function justdoitFunction() {
  const ul = document.getElementById("list");

  const listElement = document.createElement("li");
  listElement.textContent = "This is done by Panda.";
  ul.appendChild(listElement);
}


if (btnElement) {
  btnElement.addEventListener("click", justdoitFunction);
}
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!