Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with dynamically generated arguments in callback function

Status
Not open for further replies.

MigrantFirmWorker

Programmer
Apr 9, 2003
54
0
0
US
I wish to build a table and assign event callbacks with dynamically generated arguments. In the sample code below I am trying to pass the row and column value associated with the cell that is clicked. What I really need is the values of 'row' and 'col', not the variables themselves.

In the past I have set the ID of the cell to some parseable string containing all the info I needed but I'm assuming there is a more elegant approach.

Any help will be appreciated.

function cellClicked(rr, cc) {
// do something useful.​
}
function populateTable() {
var myTable = getElementById("initiallyEmptyTable");
for (var row=0; row<5; row++) {
var myRow = document.createElement("tr");
for (var col=0; col<6; col++) {
myCell = document.createElement("td");
myCell.onclick = function(){cellClicked(row, col);};
myRow.appendChild(myCell);​
}
myTable.appendChild(myRow);​
}​
}

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 
ah. a classic closure issue.

google for javascript closures. or if you want to avoid that, append a row and col property to each td. then in the callback, lookup the property for the clicked cell.

easier still to do in jquery.

 
Thanx.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top