Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter to a dynamically set JavaScript function?

Tags:

javascript

Ok, I have one JavaScript that creates rows in a table like this:

  function AddRow(text,rowID) {
    var tbl = document.getElementById('tblNotePanel');
    var row = tbl.insertRow(tbl.rows.length);

    var cell = row.insertCell();
    var textNode = document.createTextNode(text);
    cell.id = rowID;
    cell.style.backgroundColor = "gold";

    cell.onclick = clickTest;
    
    cell.appendChild(textNode);
  }

In the above function, I set the cell's onclick function to call another JavaScript function called clickTest. My question is when I assign the onclick event to call clickTest, how do I set parameter information to be sent when the clickTest method is called on the cell's onclick event? Or, how do I access the cell's ID in the clickTest function?

Thanks, Jeff

like image 899
Yttrium Avatar asked Nov 20 '08 03:11

Yttrium


1 Answers

Try this:

cell.onclick = function() { clickTest(rowID); };

The idea is that you are binding the onclick handler to the anonymous function. The anonymous function calls clickTest with rowID as the parameter.

like image 77
tvanfosson Avatar answered Oct 12 '22 03:10

tvanfosson