Tuesday, 13 February 2018

javascript - How can I pass arguments to addEventListener listener function?










for (var i = 0; i < pois.length; i++) {
pois[i].marker.addEventListener('dblclick', function (data) {
var infoWindow = new BMap.InfoWindow(pois[i].address, opts);

map.openInfoWindow(infoWindow, map.getCenter());
})
}


this is my code.
obviously there is an error at here




new BMap.InfoWindow(pois[i].address, opts);





how can I use pois[i].address in pois[i].marker listener function ?


Answer



for (var i = 0; i < pois.length; i++) {
setupEventListener(pois[i]);
}

function setupEventListener(obj) {
var address = pois[i].address;
obj.marker.addEventListener('dblclick', function (data) {

var infoWindow = new BMap.InfoWindow(address, opts);
map.openInfoWindow(infoWindow, map.getCenter());
});
}

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...