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

249
Views
Quiero hacer una lista de tareas pero estoy atascado al principio, ¿dónde me equivoco en este código?

Quiero agregar li al elemento ul con el valor ingresado por el usuario en el área de entrada. Este es el error que estoy recibiendo en la consola:

 [Error] TypeError: document.getElementsByClassName("list-group").appendChild is not a function. (In 'document.getElementsByClassName("list-group").appendChild("li")', 'document.getElementsByClassName("list-group").appendChild' is undefined) additem (script.js:7) 

 //this is my javascript code--> function additem() { var task = document.getElementsByClassName("input-group-text").value; var li = document.createElement("li"); li.classList.add("list-group-item"); li = "task"; document.getElementsByClassName("list-group").appendChild("li"); } document.getElementById("button").addEventListener("click",additem);
 //<--!this is my html code--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>LearnCodeOnline</title> <link rel="stylesheet" href="./style.css" /> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" /> </head> <body class="bg-dark"> <div class="container bg-warning p-4"> <h1 class="text-center">LearnCodeOnline</h1> <div class="input-group"> <span class="input-group-text">todo</span> <textarea class="form-control" aria-label="With textarea"></textarea> </div> <button id="button" class="float-right">Add</button> <ul class="list-group"> <li class="list-group-item">An item</li> </ul> <!-- <button type="button" class="btn btn-success btn-lg mt-4 mx-auto d-block sort-btn" > Sort courses </button> --> </div> </body> </html>

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

0

JS:-

 function additem() { var task = document.getElementsByClassName("form-control")[0].value; var li = document.createElement("li"); li.classList.add("list-group-item"); li.innerText = task; document.getElementsByClassName("list-group")[0].appendChild(li); } document.getElementById("button").addEventListener("click",additem);
  • En lugar de hacer .appendChild("li") , le daremos un elemento y no una cadena, ya que toma el elemento y no una cadena
  • getElementsByClassName() devuelve una matriz, por lo que tomaremos el elemento en el índice 0 de la matriz en ambas funciones donde está allí
  • también en lugar de configurar li = "task" como esta, tendremos que configurar su texto por li.innerText = task
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!