Ahmad Farid
Ahmad Farid

Reputation: 14764

How to get the ID of a clicked HyperLink in it's event handler in Javascript?

How to get the ID of a clicked HyperLink in it's event handler in Javascript?

Upvotes: 0

Views: 464

Answers (4)

Ketan
Ketan

Reputation: 4280

For IE use event.srcElement.id. For other browsers use event.target.id.

Upvotes: 0

Ahmad Farid
Ahmad Farid

Reputation: 14764

Actually this worked! this.event.srcElement.id

Upvotes: 0

Justin808
Justin808

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

JAAulde
JAAulde

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

Related Questions