That's a very educative event example, I must say.

You are creating a 'i' variable and using it in a loop. Each layer is given a
mouseup listener that goes

alert("I am "+i)

but when is this executing ? Not now. The method you are setting is one that
will execute onmouse up, and then 'i' will be 2, because mouseups happen after
your initialization loop. Try this one:

Events[i].onmouseup = new Function( "alert(' I am "+i+"')" )

See now each method contains a different string, because the string is
constructed on function creation, and we are not using a variable that was set
before, as it was in your example.




"GARRIS, TRAVIS" wrote:

> I hope I'm not flooding the list on my first day, but I gots to axe
> questions.
>
> Why do I get the result "I am 2" when I click on of the buttons in the
> example below?
>
> I created myText to see if I could affect objects via the array.  I can.
>
> I created Button0 and Button1 to put into an array, and try to generate
> their events in a for loop.  Button0 should change myText to "I am 0", and
> Button1 should change myText to "I am 1".  Both of them change myText to "I
> am 2".  Why?
>
> ---
> <html>
>
>   <head>
>
>     <script language='JavaScript'
> src='/JavaScripts/dynapi/src/dynapi.js'></script>
>
>     <script lanugage='JavaScript'>
>       DynAPI.setLibraryPath('/JavaScripts/dynapi/src/lib/');
>       DynAPI.include('dynapi.api.*');
>       DynAPI.include('dynapi.event.*');
>       DynAPI.include('dynapi.ext.inline.js');
>       DynAPI.include('dynapi.util.debug');
>     </script>
>
>     <script language='JavaScript'>
>
>       DynAPI.onLoad = function(){
>
>         var myText = this.document.getAll()["myText"]
>         myText.setVisible(true);
>         var myEvent = new EventListener(this.document);
>         myEvent.onmouseup = function() {
>           Buttons[0].setHTML("Touch 0");
>           Buttons[1].setHTML("Touch 1");
>         };
>         myText.addEventListener(myEvent);
>
>         var Buttons = new Array;
>         var Events = new Array;
>
>         for(var i = 0; i < 2; i++) {
>
>           Buttons[i] = this.document.getAll()["Button" + i]
>           Buttons[i].setVisible(true);
>           Events[i] = new EventListener(this.document);
>           Events[i].onmouseup = function() {
>             myText.setHTML("I am " + i);
>           };
>           Buttons[i].addEventListener(Events[i]);
>
>         }
>
>       };
>
>     </script>
>
>   </head>
>
>   <body>
>
>     <div id='myText' style='position:relative;
> visibility:hidden;'>Yeap</div>
>
>     <div id='Button0' style='position:relative; visibility:hidden;'>Button
> 0</div>
>
>     <div id='Button1' style='position:relative; visibility:hidden;'>Button
> 1</div>
>
>   </body>
>
> </html>
> ----
>
> Travis Garris, Ciber
> Winston-Salem, NC
>
> _______________________________________________
> Dynapi-Help mailing list
> [EMAIL PROTECTED]
> http://lists.sourceforge.net/lists/listinfo/dynapi-help


_______________________________________________
Dynapi-Help mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dynapi-help

Reply via email to