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

181
Vistas
Making an input that adds to a list

I am trying to make an input text and a button when pressed will add to a ul list but I keep having this error in the console and I don't know why

Uncaught TypeError: btn.addEventListener is not a function at script.js:5

Code :

var text = document.getElementsByClassName('text');
var list = document.querySelector('.list');
var btn = document.getElementsByClassName('add');

btn.addEventListener("click", function(){
    var content = document.createElement('li')
    content.innerText = text.value
    list.append(content)
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are getting this error because getElementsByClassName returns an array of all elements having the given class. To solve this error you have two options:

  1. Make add id instead of class and use getElementById to get that element.

    var btn = document.getElementById('add');

  2. If you do not want to make add id, we know getElementsByClassName returns an array you can access the elements inside this array by its index.

    var btn = document.getElementsByClassName('add')[0];

    In this case, keep in mind that the button you are trying to access with add class should be the first element with this classname.

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