locked
PRB attachEvent is not working properly RRS feed

  • Question

  • PRB attachEvent is not working properly

    I am having a weird problem. I am running a loop, to call attachEvent for rows in a table as such:

    var i;
    var trRow;
    var tblMyTable = document.getElementById("tblMyTable"); // Table has 3 rows...

    for (i = 0; i < tblMyTable.rows.length; i++)
    {
      trRow = tblMyTable.rows[i];
      trRow.attachEvent("onmouseover", function() { MyMouseOver(trRow); });
    }

    The problem is, the call to MyMouseOver is always calling it as is "trRow" was the last row (Rows #2).
    And if I change the for-loop to go from the end of the table to the start, the MyMouseOver is always called for the first row (Row #0).

    It is as-if, the call to attachEvent, is not getting "saved", or erased, in favor of the last instance it is called.

    Any ideas?
    Thursday, March 18, 2010 6:19 PM

All replies

  • for (i = 0; i < tblMyTable.rows.length; i++)
    {
     trRow = tblMyTable.rows[i];
     trRow.i = i;
     trRow.attachEvent("onmouseover", function() { MyMouseOver(tblMyTable.rows[this.i]); });
    }
    Monday, June 7, 2010 4:03 PM