[Javascript] problem with event assignment

Howard Jess howard at dhitechnologies.com
Wed Jan 18 12:12:40 CST 2006


On Tue, 17 Jan 2006 21:55:13, Konstantine wrote:

[reformatted]
> var doc = request.responseXML;
> var markers = doc.documentElement.getElementsByTagName("marker");
>     for (var i = 0; i < markers.length; i++) {
>         var p = new Point(parseFloat(markers[i].getAttribute("x")),parseFloat(markers[i].getAttribute("y")));
>         var marker = new Marker(p);
>         var popup = markers[i].firstChild.firstChild.nodeValue;
>         ATGEvent.addListener(marker, "click", function() {marker.openInfo(popup)});
>     }
>
> The problem is all markers assigned same, the last popup value. I
> think this is something to the binding time?
>  Would appreciate if you can tell me how i can work around this.
> thanks for your time.

There are only -one- (1) each of variables named "marker" and "popup", even
though they're declared within the for-loop. Their values are whatever they
were assigned in the last loop.

Try this (untested):

  var doc = request.responseXML;
  var markers = doc.documentElement.getElementsByTagName("marker");
  function addL(mrkr,popup) {
      ATGEvent.addListener(mrkr,"click",function() {mrkr.openInfo(popup)});
  }
  for (var i = 0; i < markers.length; i++) {
      var p = new Point(parseFloat(markers[i].getAttribute("x")),parseFloat(markers[i].getAttribute("y")));
      var marker = new Marker(p);
      var popup = markers[i].firstChild.firstChild.nodeValue;
      addL(marker,popup);
  }


hj



More information about the Javascript mailing list