Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda