Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
I want to make a todo list but I am stuck at the start, where am I going wrong in this code?

I want to add li to the ul element with the value entered by user in the input area. This is the error I am getting in console:

[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 Respuestas
Responde la pregunta

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);
  • Instead of doing .appendChild("li"), we'll give it an element and not string as it takes the element and not string
  • getElementsByClassName() returns an array so we will take the item at 0th index of the array in both the functions where it is there
  • also instead of setting li = "task" like this, we'll have to set its text by li.innerText = task
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda