javascript - Access variable in Google Maps Events addlistener -


for(var in coordenadas){   google.maps.event.addlistener(marker[i], 'click', togglebounce); } 

togglebonce function:

function togglebounce() {       if (this.getanimation() != null) {         this.setanimation(null);         var = "i want access of variable i"       }        else {         this.setanimation(google.maps.animation.bounce);       }     } 

in function togglebounce() how can access of variable ? variable passed in ...addlistener(marker[i]...

the simplest solution add property of marker when create (just careful naming doesn't conflict existing property of google.maps.marker).

// or here (since didn't provide code creates markers) for(var in coordenadas){   marker[i]._index = i;   google.maps.event.addlistener(marker[i], 'click', togglebounce); }  function togglebounce() {   if (this.getanimation() != null) {     this.setanimation(null);     var = this._index;   }    else {     this.setanimation(google.maps.animation.bounce);   } } 

proof of concept fiddle


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -