This registers an user defined javascript event listener.
This function allows you to:
- add multiple event listeners for the same node and event type (e.g. you can set forms[0].onsubmit to trigger multiple functions)
- add an event listener to all tags of a certain name (e.g. add an input handling function to the "onchange" event of all textarea tags)
The argument $eventType defines the type of event to subscribe to. It can be any of the following:
- onabort
- onblur
- onchange
- onclick
- ondblclick
- onerror
- onfocus
- onkeydown
- onkeypress
- onkeyup
- onload
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onreset
- onselect
- onsubmit
- onunload
Note that - even though you may set an event handler on any node - some of them are limited to certain tags. E.g. "onsubmit" is limited to "form" tags. See your favourite JavaScript reference for more details.
You are best adviced NOT to use proprietary event types like "ondrag" or "oncontextmenu". However: if you do, the function will present you with a warning. To surpress this warning, set the argument $silent to bool(true).
The argument $userFunction is an existing function name. Note that this function will be given 2 arguments. The first is the event object. The second is a reference to the target node. (While you may also get the target node using event.target in Firefox and Opera, this is not supported in Internet Explorer. So this should work in both)
A brief example:
function myEventHandler($event, $node)
{
if ($event.type == 'change') {
alert("New value = " + $node.value);
return true;
} else {
return false;
}
}
Note that returning bool(false) will stop propagation of the event.
The argument $node has 2 synopsis.
- If $node is a node object, like document.forms[0], the event listener listens to events on this node only.
- If $node is a tag name, like 'form', the event listener listens to all nodes which have the specified tag name.
Note that this function respects event handlers defined in the HTML code.
Where multiple event handlers are present, the behaviour is as follows:
- call event handler defined in HTML code (if any)
- call any user defined event handler in the order in which they were registered
Note: if any of the called functions return a value that evaluates to bool(false), the event handler will stop propagation of the event and return bool(false). E.g. when handling form.onsubmit with multiple functions, that all checking the form's contents, then the form will not submit if ANY of the included checks fails.
Returns bool(true) on success and bool(false) on error.