function myFunction(par) {
...
}
el1.onclick=myFunction(5);
el2.onclick=myFunction(7);
How to get a reference for the caller element inside myFunction?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
//this is saying call this function and asign what it returns to onclick
el1.onclick= myFunction(5);
You want to use a closure
el1.onclick = function(){myFunction(5);}
To reference the element you can pass the object in
example 1
or you can use call so that within the listener,
thisreferences the element.example 2