I’ve got a few DOM elements, say text-field input A, B, C, and all of them have the property of ‘onclick’. How do I find to which object a particular ‘onclick’ belongs?
Share
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.
You’re probably trying to find the element that triggered an event handler.If so, use the
thiskeyword.EDIT
Apparently, you have an inline event handler as a string, and you’re trying to find the element that the handler was attached to, from the string.
This is completely impossible.
A string is just a string. Just because your string happens to contain Javascript code, and happens to be assigned to a property on an object, doesn’t make it any different from any other string; Javascript does not track parent references in objects.
Instead, you should bind an anonymous function as an event handler using jQuery.
(Or, if you really don’t want to use jQuery,
addEventListener/attachEvent)