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

173
Views
¿Cómo puedo asignar una función JS a una lista de etiquetas que se generan con un bucle?

Tengo un bucle en mi archivo twig:

 {% for item in listItem %} <h2 class="title" id="name-id"> ID {{ item.getRobotName() }}</h2> <p id="latbot" data-lat="lat">{{ item.geteLat() }}</p> <p id="lonbot" data-lon="lon">{{ item.getLon() }}</p> {% endfor %}

En mi archivo Javascript tengo:

 let h2 = document.getElementById("name-id"); h2.addEventListener("click", link); function link() { document.getElementById("name-id"); let thisLat = document.getElementById('latbot').getAttribute('data-lat'); console.log(thisLat); let thisLon = document.getElementById('lonbot').getAttribute('data-lon') console.log(thisLon); let notifyIcon = L.divIcon({ className: 'notify-icon', iconSize: [25, 25], html: '<span></span>' }); map.panTo([thisLat, thisLon]); L.marker([thisLat,thisLon], {icon: notifyIcon}).addTo(map); } linkToMarker();

Me gustaría asignar una función JS a todos los h2 que se generan con este bucle for. Cómo puedo hacer eso ?

Solo el primer elemento está conectado a la función JS pero no los demás

Gracias.

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!