EventTarget.addEventListener() (Method)

Adds a listener function to handle events dispatched to the owning EventTarget node.

Availability:

DOM level - 2
JavaScript - 1.5
Netscape - 6.0
JavaScript syntax:NmyEventTarget.addEventListener(aType, aListener, aFlag)
Argument list:aTypeA string containing the event type
aListenerA reference to an EventListener function object
aFlagA Boolean value containing the useCapture dispostion

The addEventListener() method will attach a function object to an EventTarget node in such a way that the function is called when the event is triggered. An Event object is passed to the function as an argument so you need to specify this in its declaration. This is how handler functions are registered as listeners.

You can replace a listener with another by registering a new listener with the same parameters. The old one will be discarded. If you want to stop the listener from responding to the event, you can deregister it with the removeEventListener() method.

The three parameters to the addEventListener() method are:

Examining the DOM level 2 specification suggests that these event type strings may be used:

Event stringEvent object typeDOM
abortHTMLEvent2
blurHTMLEvent2
changeHTMLEvent2
clickMouseEvent2
DOMActivateUIEvent2
DOMAttrModifiedMutationEvent2
DOMCharacterDataModifiedMutationEvent2
DOMFocusInUIEvent2
DOMFocusOutUIEvent2
DOMNodeInsertedMutationEvent2
DOMNodeInsertedIntoDocumentMutationEvent2
DOMNodeRemovedMutationEvent2
DOMNodeRemovedFromDocumentMutationEvent2
DOMSubtreModifiedMutationEvent2
errorHTMLEvent2
focusHTMLEvent2
loadHTMLEvent2
mousedownMouseEvent2
mousemoveMouseEvent2
mouseoutMouseEvent2
mouseoverMouseEvent2
mouseupMouseEvent2
resetHTMLEvent2
resizeHTMLEvent2
scrollHTMLEvent2
selectHTMLEvent2
submitHTMLEvent2
unloadHTMLEvent2

Note that the DOM level 2 event module specification is somewhat ambiguous on the specifics of event type values and you may want to refer to the specification if you are intending to exercise these capabilities in the Netscape Navigator version 6.0 browser. This functionality is fairly new and still in a state of evolution but since it is defined in a standard, it will become more widespread and is therefore the best way to ensure portability even if it takes a while for the various browser manufacturers to catch up and make their products compliant.

See also:Element.onevent, EventListener object, EventTarget.removeEventListener(), MutationEvent object