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

128
Views
Referencing a yet to be created element in another element's event listener in JavaScript

I have an element created dynamically, for which I have already attached an event listener to, like below:

radio.addEventListener('click', (event) => {
   document.getElementById(action[1]).style.display = 'block';
});

The 'action[1]' is a string which will be the id of an element yet to be created. I have tried this, and it seems to work. I know the event listener's function will only be fired on the 'click' event, and by then the 'action[1]' element will exist. But it seems like I'm attaching an invalid function when the addEventListener is attached to my 'radio' element. Will this cause problems?

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

0

Okay, I want to close this with an answer, but the answer, comes from Barmar, so I don't take credit for it. As stated in the comment,

The body of a function isn't evaluated until the function is called. There's no problem with access variables that are assigned between adding the event listener and triggering the event.

Question is answered.

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!