Elementals.js

Latest Version: 3.7 Final 20 December 2018

Method _.Event.add

Summary:

Attaches an event to an Element in a cross-browser compatible manner. The resulting event will pass the Event object to the callback even in older versions of Internet Explorer, and that Event will have correct Event.target and Event.currentTarget values.

Calling Convention:
_.Event.add(Element, event, callback[, captureLevel[, forceThis]])
Parameters:
Element
The Element you want the event attached to
event
The name of the JavaScript event you want to hook, such as "change", "submit', or "over".
callback
The function to be called when the event occurs.
captureLevel Optional

The amount of bubbling/propagation you want the callback to prevent. The values are as follows:

0 or default/omitted
Same behavior as Element.addEventListener when useDefault is false.
1
Same behavior as Element.addEventListener when useDefault is true. (experimental support in legacy IE, needs more testing · 14 July 2017)
2 or higher
As per 1, but in modern browsers Event.stopPropagation and Event.preventDefault are called if they exist. In legacy versions of IE Event.cancelBubble is set to true, and Event.returnValue is set to false.
forceThis Optional

Default: NULL

All event callbacks are handled using the Function.call() method. If you provide this optional parameter it will be used instead of NULL as the first argument allowing you to set what this will be inside your callback function.

Returns:
Nothing

Example

JavaScript

_.Event.add(
	document.getElementById('testForm'),
	'submit',
	function(e) {
		console.log('attempted to send ', e.currentTarget);
	},
	2
);

Advertisement