G'day all...

I have an array of divs. which are held by another object (lets call it X).

In the X' constructor, the div array is gone through, and an EventListener
is added to each one, but I want to access the for loops index from inside
the div's EventListener.

The problem is the code for the div's EventListener is evaluated /after/ the
for loop has executed and hence is always making an invalid to reference.

Eg.
// Inside constructor
for (i = 0; i < divArray.length; i++)
{
        divListener = new EventListener(divArray[i]);
        divLittener.onmouseover = function(e)
        {
                target = e.getTarget();
                target.setHTML("This is div#: " + i);
        }
        divLittener.onmouseoff = function(e)
        {
                target = e.getTarget();
                target.setHTML(" &nbsp; ");
        }
}

So if there are 5 divs in the array, the HTML will always be "This is div#:
5" for each of the div's.

Is there a way to pass the for loops index as a parameter to the
EventListener at the for loops execution time, rather than the
EventListener's execution time?

Currently I'm using a kludge which looks like:

// Inside constructor
for (i = 0; i < divArray.length; i++)
{
        divArray[i].setHTML("<-- " + i + " -->");
        divListener = new EventListener(divArray[i]);
        divLittener.onmouseover = function(e)
        {
                target.setHTML(theHTML.substring(pos1, pos2+4) + "This is div#: " + x);
        }
        divLittener.onmouseoff = function(e)
        {
                target = e.getTarget();
                theHTML = target.getHTML();
                pos1 = theHTML.indexOf("<!-- ") + 5;
                pos2 = theHTML.indexOf(" -->");
                distance = pos2 - pos1;
                x = parseInt(theHTML.substr(pos1, distance));

                target.setHTML(theHTML.substring(pos1, pos2+4) + " &nbsp; ");
        }
}

Please tell me there is an easier way... :)

All the best...

Michael.


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

Reply via email to