Possible Duplicate:
How to pass arguments to an event handler function and still use ‘this’ reference?
I've done a bit of searching but I'm struggling to find a solution to this.
Anyway:
function CreateMe(){
//....
var mycell = row.insertCell(-1);
var myelement3 = document.createmyelement("input");
myelement3.type = "button";
myelement3.value = "Delete";
myelement3.onclick = DeleteMe;
myelement3.name = "Delete[]";
mycell.appendChild(myelement3);
//....
}
function DeleteMe(){
alert(this);
}
I have a form with a button that calls CreateMe which dynamically creates a button. What I'm after is to be able to pass in arguments to the DeleteMe function when this dynamic button is clicked; in particular this:
onclick="DeleteMe(this.parentNode.parentNode.rowIndex)"
from: http://www.java2s.com/Code/JavaScript/HTML/Deletingtablerows.htm
I'm at a bit of a loss on how to proceed here, so if anybody could lend a hand it would be appreciated.
Edit: Just to be clear, what is being asked is how I can pass arguments from the onclick event of the dynamically generated button to the DeleteMe function. Clearly I am unable to do this as this would simply evaluate the function immediately:
myelement3.value = "Delete";
myelement3.onclick=DeleteMe(this.parentNode.parentNode.rowIndex);
myelement3.name = "Delete[]";
javascript event handler pass argument.onclickproperty:element.onclick = function() {...}. And how the function could look like can be found in the other two questions ;) edit: See, you are nearly there, you just have to do:myelement3.onclick = function() { DeleteMe(this.parentNode.parentNode.rowIndex); };, or use.callto also setthisproperly if you need it, just as shown in stackoverflow.com/q/9202339/218196.