JavaScript event.target and event.srcElement
Question: How do I determine the event's target element?
Answer:
Mouse events of all types (e.g. To determine the event's target element, your JavaScript event handler functions can use the following event properties:
Example:
The event handler function function clickHandler(e){ var elem, evt = e ? e:event; if (evt.srcElement) elem = evt.srcElement; else if (evt.target) elem = evt.target; alert ('' +'You clicked the following HTML element: \n <' +elem.tagName.toUpperCase() +'>' ) return true; } document.onclick=clickHandler; Demo: click anywhere on this page to see this event handler in action!
Note:
Event target element may be the same element that invoked the event handler.
However, the event handler may also be invoked for a parent or ancestor element of the target.
(This may happen due to event bubbling.
For example, the above event handler function See also: |
Copyright © 1999-2011, JavaScripter.net.