Tag Archives: target

Javascript: Event.target and Event.srcElement

When working with events and making your code compatible with other browsers it’s important to remember to use the right target property depending what browser you are in.

For example if you are using Firefox then you can simple do this:

This will output an element which trigger the event. Now if you want to achieve the same thing in Internet Explorer (versions 6-8) you have to use different approach because event listeners are attached using a non-standard element.attachEvent() method.

To make things shorter and better, I recommend using a short notation to target both situations at the same time depending on the browser the client is using, like this:

You can read up more about this here.