Edit (torazaburo was correct):
For future reference, and since there appears to be a lot of confusion about closures by many who commented, here are some notes.
You can write the code as I did below, in fact it was correct. The problem was, I simplified my specific case too much. It was not the way I wrote the closure. My problem was the DOM element was a table cell td, which had not been appended to the row (newRow.appendChild(newCol)) at that specific point in time when I was adding the hover. Consequently the cell was not in the DOM at that point in time. Consequently jQuery did not know it existed.
The original question:
My code:
var i, strId;
for(i=0;i<10;i++){
// strId is the id of the element without the # needed for jquery
strId = "idString" + i.toString();
// Some stuff ...
// It is done this way so the value of i is retained in the functions in the hover
(function (i, strId) {
$("#" + strId).hover(function () { MyFunctionIn(i);}, function () { MyFunctionOut(i); });
})(i, strId);
}
This does not work as written.
The value of i is being retained, as I have tested this with just $("#idString0") in place of $("#" + strId) and this works fine.
But when I put the variable strId back into the hover it fails.
Anyone know what I am doing wrong?