Posted By

noah on 04/30/07


Tagged

DOM click events ui eventhandlers onclick quirksmodeorg ppk


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

tylerhall
vali29
heinz1959
SpinZ
hans
ckayra
seanpowell
guo3823538


Which element was clicked?


 / Published in: JavaScript
 

How to get a reference to the element that was clicked to fire an event. The code is gratefully cribbed from Quirksmode.org.

Updated to reflect how I actually implement this, slightly different from PPK's original.

  1. <html>
  2. <head>
  3. <script>
  4. /* http://quirksmode.org */
  5. function getEvent (e) {
  6. var event = e || window.event;
  7. if( ! event.target ) {
  8. event.target = event.srcElement
  9. }
  10. return event;
  11. }
  12. </script>
  13.  
  14. </head>
  15. <body>
  16. <a href="#fred" id="fred" onclick="alert(getEvent(event).target)">foo</a>
  17. </body>
  18. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: frontendengineer on May 4, 2012

This returns the href value of the tag not the html element tag.

check here

http://tinkerbin.com/IMfx83Su

You need to login to post a comment.