I have html like below:
<div onclick=" callJavascript()">
<span id="abc"> abc </span>
<span id="def"> def </span>
<span id="ghi"> ghi </span>
</div>
When I click ‘abc’ or ‘ghi’, is there anywhere inside function callJavascript(), to find out which span id I clicked on ?
Yes there is :
For Not IE browsers :
For IE :
You must nevertheless adapt your html :
If you want a more complete reference, here it is : http://www.quirksmode.org/js/events_properties.html
Wow ! May I mention, for better accuracy, your event listener should not be on the div element, instead, it may be triggered by the span elements themselves. The target or srcElement being the one on which the event occurs.
It’s not part of the question, but it’s worth saying : I usually prefer to avoid event attachment in html code since it causes scope problems and forces you, as in your case, to manually pass the event parameter.