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

114
Views
Adding an event listener for each element created by a foreach loop

Have got data from an api call and have looped through it to create elements on the page. What I can't figure out how to do is generate an event listener for each button that is created. I'm not sure I'm using the right id to get each button. Apologies I'm very new to coding.

$('.btn btn-primary').on('click', () => {
    // some function
});
cities.forEach((city) => {
                $("#citiesContainer").append(`
                <div class="citiesRow" class="row">
                    <div class="col-8"><h5>${city.name}</h5></div>
                    <div class="col-4">
                        <button value=${city.id} type="button" class="btn btn-primary">Primary</button>
                    </div>
                    
                </div>
                `);
            });

about 4 years ago · Juan Pablo Isaza
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!