Possible Duplicate:
I trying to get the target element’s id when function fired, butthiskeyword returnsundefined
— HTML file —
<a href="#link_1" id="id_0" onclick = "getElement('parms1', 'parms2', 'parms3')">0</a>
<a href="#link_2" id="id_1" onclick = "getElement('parms1', 'parms2', 'parms3')">1</a>
<a href="#link_1" id="id_2" onclick = "getElement('parms1', 'parms2', 'parms3')">2</a>
— Js file —
function getElement = function(a, b, c){
// trying to get the clicked element
alert(this); // returns 'undefined'
}
Note: I can’t modify / add / remove parameters in onclick function, but I can add / remove parameters in js file
Problem: How to identify that which element was clicked without modifying the onclick function parameters?
Simple fix:
Change the HTML to:
And the function definition to:
So the choice is yours, either pass
thisandeventto the function, or justevent, or justthisand you’re good to go.You might want to look into delegating this type of events, though