Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
How can I assign a JS function to a list of tag that are generate with a loop?

I have a loop in my twig file :

{% 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 %}

In my Javascript file I have:

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();

I would like to assign a JS function to all the h2 that are generate with this for loop. How can I do that ?

Only the first item is connected to the JS function but not the others

Thank you.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda