Elementals.js

Latest Version: 3.0 RC 1 17 July 2017

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])
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. [i](experimental support in legacy IE, needs more testing · 14 July 2017)[/i]
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 iss set to false.
Returns:
Nothing

Example

JavaScript

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

Advertisement