Steven Lu
Steven Lu

Reputation: 43427

inline javascript: can it retrieve reference to the element it was invoked from?

On basically any element you can put a onclick='func()' or some such to run JS when interacted with. Is there any way to get a reference to the DOM element itself?

When i do the obvious thing: <input type='button' onclick='alert(self);' /> I get [Object DOMWindow], which isn't quite as convenient as I'd hoped.

I realize I can easily hack my way through it by using id's or classes. But that means for each new custom-code button i create I must ensure it has a unique self referencing id, which I'd like to do without because that is more difficult to maintain.

Upvotes: 0

Views: 205

Answers (1)

PleaseStand
PleaseStand

Reputation: 32082

Inline event handlers embedded in the HTML are called with this being the element in question.

<input type='button' onclick='alert(this);' />

Upvotes: 4

Related Questions