function addListener(elem, type, fn, capturingPhase) { if (elem.addEventListener) { elem.addEventListener(type, fn, capturingPhase); } else if (elem.attachEvent) { // IE8...IE6. Bubbling only elem.attachEvent("on" + type, function() { return fn.call(elem, window.event); }); } else { // IE5-. Bubbling only elem["on" + type] = fn; } }
Functions and Properties of Event Object.
Name | Description | Returns |
---|---|---|
type | To specify the name of the event.(i.e click, mouseout) | string |
target | The element at which the event is to be triggered | HTMLElement |
currentTarget | Current element whose event listeners are currently being invoked. | HTMLElement |
eventPhase | The phase in the life cycle of an event. | number |
bubbles | A true value is returned if the event will bubble through the document, or else a false is returned. | boolean |
cancelable | A true value is returned if the event has a default action that can be cancelled, otherwise a false is returned. | boolean |
timeStamp | To denote the time at which the event was created, if no time is available then 0 is returned. | string |
stopPropogation() | To cancel any further event capturing or event bubbling. Can be used if bubbles is true. | void |
stopImmediatePropogation() | It cancels any further event capturing or event bubbling and prevents any other event handlers from being called. | void |
preventDefault() | It prevents the browser from performing the default action associated with the event | void |
defaultPrevented | A true value is returned if the preventDefault() has been called. | boolean |