DOMAssistantEvents Module

The DOMAssistantEvents module offers various methods for adding and removing handlers for one or several events on an element, using either traditional event handling or event delegation. It also contains functionality for stopping default actions and bubbling of events.

addEvent(evt, func)

Adds an event handler to the current element. Multiple event handlers are supported, and the receiving funtion will have an event object reference and a this reference to the element it occurred on, no matter what web browser. For accessibility reasons, please make sure to only apply click events to elements that can handle them without JavaScript enabled.

Parameters

evt
Event to apply, specified as a string, without the "on" prefix. Custom events are acceptable.
func
Function to handle the event, specified as a function reference (without parentheses) or an anonymous function.

Return value

Element which called the method.

Example calls

$("container").addEvent("click", getListing);

$("container").addEvent("dblclick", function () {
alert("Hello darling!");
});

removeEvent(evt, func)

Removes either a specific event handler or all of them from the current element.

Parameters

evt
Event to remove, specified as a string, without the "on" prefix. Optional. If unspecified, handlers for all events (including inline events) will be removed.
func
Function to stop from handling the event, specified as a function reference (without parentheses). Optional. If unspecified, all handlers (including inline event handlers) for the event will be removed.

Return value

Element which called the method.

Example calls

$("container").removeEvent("click", getListing);

// To remove all events bound to the odd row, either do this
$("tr:nth-child(odd)").removeEvent("mouseover").removeEvent("mouseout");

// Or do this
$("tr:nth-child(odd)").removeEvent();

relayEvent(evt, selector, func)

Adds a centralized event handler to the current element. Events that occur on elements matching the selector bubble up and get handled in the current element. Other than traditionally bubbling events, others like focus, blur, submit, reset, change and select are also supported.

Parameters

evt
Event to apply, specified as a string, without the "on" prefix. Custom events are acceptable.
selector
Actual targets that the event should occur on, specified as a CSS selectors.
func
Function to handle the event, specified as a function reference (without parentheses) or an anonymous function.

Return value

Element which called the method.

Example calls

$("ul").relayEvent("click", "li", jumpToPage);

$("form").relayEvent("focus", "input[type=text]", function() {
this.addClass("yellow");
});

unrelayEvent(evt)

Removes all relayed events of the specific type from the current element.

Parameters

evt
Event to remove, specified as a string, without the "on" prefix.

Return value

Element which called the method.

Example calls

$("ul").unrelayEvent("click");

triggerEvent(evt, target)

Triggers an event on the current element, and optionally sets the target to which the event is dispatched to. Note that the actual event does not happen - this method merely triggers the event handlers.

Parameters

evt
Event to trigger, specified as a string, without the "on" prefix. Custom events are acceptable.
target
The target element to which the event is dispatched. Optional.

Return value

Element which called the method.

Example calls

$("news").triggerEvent("click");

$("home-link").triggerEvent("customevent", elementRef);

preventDefault(evt)

Prevents the default action of an event. Can be called from any function, and is not a method of any element.

Parameters

evt
Event to prevent the default action of.

Return value

None.

Example calls

DOMAssistant.preventDefault(eventReference);

cancelBubble(evt)

Cancels the bubbling of an event. Can be called from any function, and is not a method of any element.

Parameters

evt
Event to cancel the bubbling of.

Return value

None.

Example calls

DOMAssistant.cancelBubble(eventReference);

Get your copy