home / software / tips and tricks / How to attach a JavaScript event handler on the fly, using JavaScript?

How to attach a JavaScript event handler on the fly, using JavaScript?

Updated:  11/01/2012 11:11 AM
Author:  Shiju Mathews

Status:    Resolved.


On the fly we can registers an event handler function (event listener) for the specified event on an object.


addEventListener(): Allows the registration of event listeners on the event target for all browsers except IE before version 9.
Syntax: object.addEventListener (eventName, function, useCapture);

attachEvent():Allows the registration of event listeners on the event target for IE before version 9.
Syntax: object.attachEvent (eventName, function);


The same way we can unregister (detach) an event handler function (event listener) for the specified event on an object.


removeEventListener(): Allows to unregistrater the event listeners on the event target for all browsers except IE before version 9.
Syntax: object.removeEventListener(eventName, function, useCapture);

detachEvent():Allows to unregistrater the event listeners on the event target for IE before version 9.
Syntax: object.detachEvent(eventName, function);

Code Sample


Tags: How to attach a JavaScript event handler on the fly using JavaScript?
Updated on: October 2024