Tuesday, February 13, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...