M
M
maestro072019-06-17 15:04:37
JavaScript
maestro07, 2019-06-17 15:04:37

How to listen for a click in child divs?

I am using 2gis in a project. 2gis draws div elements by itself.
There are markers on the map (popup). I need to find out exactly which marker was clicked?

let list = document.getElementById('mapwrapper').getElementsByClassName('leaflet-marker-pane')[0].getElementsByClassName('leaflet-interactive');
console.log(list);

let tmp = document.getElementById('mapwrapper').getElementsByClassName('leaflet-marker-pane')[0].getElementsByClassName('leaflet-interactive')[0].addEventListener('click', e => {
        console.log(e);
        console.log(e.target);
});

there are N divs in the leaflet-interactive div. When clicking on a marker, when a popup opens, the dg-customization__marker_disappear class is added.
How to bind addEventListener for child div?
and in general, how can you implement a handler on div elements in vue?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2019-06-17
@hzzzzl

do these divs have some kind of common selector by which they can be caught?
in extreme cases, it will probably work on the selector "div.leaflet-interactive > div" or something like that

document.body.addEventListener('click', e => {
  if(e.target.matches('.super-selector')) { 
    // e.target - кликнутый элемент
  }
})

A
Anton Anton, 2019-06-17
@Fragster

Perhaps you can get away with listening for a click on the parent element? Read about the useCapture parameter of addEventListener https://habr.com/en/post/126471/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question