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

182
Views
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 answers
Answer question

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 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!