Reputation: 14764
How to get the ID of a clicked HyperLink in it's event handler in Javascript?
Upvotes: 0
Views: 464
Reputation: 4280
For IE use event.srcElement.id. For other browsers use event.target.id.
Upvotes: 0
Reputation: 21512
A) <a id="MyID" href="javascript:alert(this.id);">Doesn't Work</a>
B) <a id="MyID" href="#" onclick="alert(this.id);">Works</a>
Not sure how to make option A work.
update As JAAulde noted in a comment. you really shouldn't use option A.
Upvotes: 0
Reputation: 19560
Inside of the event handling function, this
is the element that was actually clicked. So accessing this.id
is what you need.
Upvotes: 2