INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Problem with dynamically generated arguments in callback function

Problem with dynamically generated arguments in callback function

Problem with dynamically generated arguments in callback function

(OP)
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."

RE: Problem with dynamically generated arguments in callback function

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.

RE: Problem with dynamically generated arguments in callback function

(OP)
Thanx.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close