Answer the question
In order to leave comments, you need to log in
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);
});
Answer the question
In order to leave comments, you need to log in
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 - кликнутый элемент
}
})
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 questionAsk a Question
731 491 924 answers to any question