I have a table in my HTML called id='myTable'. I am then filling in the rows on-the-fly in the JavaScript (because it will constantly be changing throughout the use of the program.)
Elsewhere in the code is the function:
function OnClickRowMyTable(row){
alert("You clicked row " + row.toString());
}
Then I am constructing the table:
table = document.getElementById("myTable");
for (i = 0; i < 10; i++) {
newRow = document.createElement('tr');
newRow.setAttribute('style', "background-color: #FFFFBB");
newRow.title = "";
newRow.onclick = (function () { var iUse = i; return OnClickRowMyTable(iUse); })();
table.appendChild(newRow);
for (j = 0; j < 5; j++) {
newCol = document.createElement('td');
newCol.innerHTML = "-";
newRow.appendChild(newCol);
}
}
If I leave the (); on the end of the onclick line, it actually calls the function OnClickRowMyTable several times as the table is constructed.
If I end that line without the (), then the function is not called during construction, but when I do click the row it always says I have clicked row 10, irrespective of the row I have clicked.
How do I stop the function being called during construction, and then say I have clicked the correct row, when i click it, rather than say I have clicked row 10 all the time?