Query - Events API reference for Query methods related to event handling zap API Reference
Categories

Query - Events

Attach, remove, and trigger events on elements.

Binding Events

on

$('selector').on(eventName, handler, options);

Attaches an event listener to matched elements.

Parameters

NameTypeDescription
eventNamestringSpace-separated event types (e.g., 'click', 'mouseenter mouseleave')
handlerfunctionEvent handler function
optionsobjectOptional configuration
Options
NameTypeDefaultDescription
returnHandlerbooleanfalseReturn handler object instead of Query
abortControllerAbortControllerautoCustom AbortController for removal
capturebooleanfalseUse capture phase
oncebooleanfalseRemove after first execution
passivebooleanfalseHandler won’t call preventDefault()

Returns

  • Standard: Query object (for chaining)
  • returnHandler: true: Handler object with abort() method (example)

Usage

$('button').on('click', (e) => {
console.log('Clicked!', e.target);
});

Example

off

$('selector').off(eventName, handler);

Removes event handlers attached with on().

Parameters

NameTypeDescription
eventNamestringSpace-separated event types to remove
handlerfunctionThe handler function to remove

Returns

Query object (for chaining).

Usage

const handler = (e) => console.log('Clicked!');
$('button').on('click', handler);
$('button').off('click', handler);

Example

one

$('selector').one(eventName, handler);

Attaches a handler that executes at most once per element.

Parameters

NameTypeDescription
eventNamestringSpace-separated event types
handlerfunctionEvent handler function

Returns

Query object (for chaining).

Usage

$('button').one('click', () => {
console.log('This fires once only');
});

Example

onNext

$('selector').onNext(eventName);
$('selector').onNext(eventName, delegateSelector);
$('selector').onNext(eventName, options);

Returns a Promise that resolves on the next event occurrence. Useful for async/await patterns.

Parameters

NameTypeDescription
eventNamestringSpace-separated event types
delegateSelectorstringOptional selector for delegation
optionsobjectOptional configuration
options.timeoutnumberTimeout in ms (rejects if exceeded)

Returns

Promise that resolves with the event object.

Usage

await $('button').onNext('click');
console.log('Button was clicked!');

Event Delegation

Event delegation attaches a handler to a parent element that fires when descendants match a selector. Works with dynamically added elements.

on (delegated)

$('selector').on(eventName, delegateSelector, handler, options);

Parameters

NameTypeDescription
eventNamestringSpace-separated event types
delegateSelectorstringCSS selector to match against event target
handlerfunctionEvent handler (this bound to matched element)
optionsobjectSame options as direct binding

Returns

Query object (for chaining).

Usage

$('#container').on('click', 'button', (e) => {
console.log('Button clicked!', e.target);
});

Example

Triggering Events

trigger

$('selector').trigger(eventName);

Executes all handlers for the given event type.

Parameters

NameTypeDescription
eventNamestringEvent type to trigger

Returns

Query object (for chaining).

Usage

$('button').trigger('click');

Example

dispatchEvent

$('selector').dispatchEvent(eventName, eventData, eventSettings);

Creates and dispatches a custom event.

Parameters

NameTypeDescription
eventNamestringCustom event name
eventDataobjectData passed via event.detail
eventSettingsobjectEvent configuration (bubbles, cancelable, etc.)

Returns

Query object (for chaining).

Usage

Dispatch
$('.container').dispatchEvent('resize', { delta: 100 });
Listen
$('.container').on('resize', (e) => {
console.log('Delta:', e.detail.delta);
});

Example

Shorthand

focus

$('selector').focus();

Gives focus to the first matched element.

Returns

Query object (for chaining).

Usage

$('input.username').focus();

Example

blur

$('selector').blur();

Removes focus from the first matched element.

Returns

Query object (for chaining).

Usage

$(document.activeElement).blur();

Example

click

$('selector').click();

Triggers a click event on matched elements.

Returns

Query object (for chaining).

Usage

$('button.submit').click();

Example

submit

$('selector').submit();

Triggers a submit event on matched form elements.

Returns

Query object (for chaining).

Usage

$('form').submit();

Example

ready

$(document).ready(handler);

Executes a function when the DOM is fully loaded.

Parameters

NameTypeDescription
handlerfunctionFunction to execute when DOM is ready

Returns

Query object (for chaining).

Usage

$(document).ready(() => {
console.log('DOM ready!');
});

Example

Previous
DOM Access
Next
Iterators