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

191
Views
addEventListener not working for dynamic button

I have a dynamically made button, i've set the id and class to try to set an addEventListener but the addEventListener is not read in the DOM but the ID and class is.

here is my code

 let button: HTMLButtonElement = document.createElement('button')

  button.id = "comment-button";
  button.className = "comment-button"; 

(ID & class is working)

Here is what i've tried:

  button.addEventListener("click", setComment);

  document.getElementById("comment-button")?.addEventListener("click", setComment);

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

0

To retrieve an element from the document, you need to actually add the element you created to the document. document.body.appendChild(button)

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!