user01 user01 - 1 month ago 7
HTML Question

How to pass the id of an element that triggers an `onclick` event to the event handling function

How do I pass the id of an element that triggers an

onclick
event to the event handling function.

I am doing something like this-

<link onclick="doWithThisElement(id_of_this_element)" />

Answer

Instead of passing the ID, you can just pass the element itself:

<link onclick="doWithThisElement(this)" />

Or, if you insist on passing the ID:

<link onclick="doWithThisElement(this.id)" />

Here's the JSFiddle Demo: http://jsfiddle.net/dRkuv/