
Enter a search term above to see results...
Enter a search term above to see results...
The Event methods in Query provide tools for attaching event listeners, triggering custom events, removing them, and triggering events.
on
provides various methods to attach event listeners to DOM elements. It supports standard event binding, event delegation, and special handling for shadow DOM.
$(target).on(eventName, handler, options)
Name | Type | Description |
---|---|---|
eventName | string | One or more space-separated event types and optional namespaces |
handler | function | object | The event handler function, or additional options |
options | object | An object containing additional options |
$(target).on(eventName, selector, handler, options)
Name | Type | Description |
---|---|---|
eventName | string | One or more space-separated event types and optional namespaces |
selector | string | A selector string to filter the descendants of the selected elements that trigger the event, or the event handler function |
handler | function | The event handler function, or additional options |
options | object | An object containing additional options |
Name | Type | Default | Description |
---|---|---|---|
returnHandler | boolean | false | If true, returns the event handler object instead of the Query object |
abortController | AbortController | new AbortController() | Custom AbortController for the event listener |
capture | boolean | false | Use capture phase for event listener |
once | boolean | false | Remove listener after it executes once |
passive | boolean | false | Indicates that the function will never call preventDefault() |
Query object (for chaining).
When returnHandler
in options
is set to true
, the on
method returns an event handler object (or an array of objects for multiple elements) with the following properties:
Property | Type | Description |
---|---|---|
el | Element | The DOM element the event is attached to |
eventName | string | The name of the event |
eventListener | function | The actual function attached as the event listener |
abortController | AbortController | The AbortController for this event listener |
delegated | boolean | Whether this is a delegated event listener |
handler | function | The original handler function provided |
abort | function | A function to abort (remove) this event listener |
Attach a click event to all div elements:
import { $ } from '@semantic-ui/query';
$('div').on('click', function(event) { console.log('Div clicked!', this);});
Attach a click event to all current and future buttons within a container
When using event delegation, the handler is called with
this
set to the target element that matched the selector, not the element the listener was attached to.
Event Delegation Event delegation allow you to add or remove elements from the DOM and still have them trigger the event so long as the parent element remains in the page.
import { $ } from '@semantic-ui/query';
$('#container').on('click', 'button', function(event) { console.log('Button clicked!', this);});
When working with Shadow DOM, use the $$
function to pierce shadow boundaries:
import { $$ } from '@semantic-ui/query';
$$('my-custom-element').on('click', 'button', function(event) { console.log('Shadow DOM button clicked!', this);});
Composed Events: The
on
method automatically handles delegation targets across shadow boundaries like wbe components usingevent.composed
andevent.composedPath()
for events that cross shadow DOM boundaries. This allows the delegation to work correctly even when the target element is within a shadow root.
You can pass additional options to the underlying addEventListener
method:
import { $ } from '@semantic-ui/query';
$('div').on('click', { capture: true, once: true}, function(event) { console.log('This will only fire once, and in the capture phase');});
You can get a reference to the event handler, which includes an abort method:
import { $ } from '@semantic-ui/query';
const handler = $('button').on('click', function() { console.log('Button clicked!');}, { returnHandler: true });
// Later, to remove the event listener:handler.abort();
Here’s an example of dispatching a custom event from one element and listening to it on another:
import { $ } from '@semantic-ui/query';
// listen to a custom event$('my-component').on('customEvent', function(event) { console.log('Custom event fired!', event.detail);});
// Dispatch the custom event$('my-component').dispatchEvent('customEvent', { detail: 'Hello from custom event!' });
You can provide your own AbortController for more fine-grained control:
import { $ } from '@semantic-ui/query';
const controller = new AbortController();
$('button').on('click', function() { console.log('Button clicked!');}, { abortController: controller });
// Later, to remove the event listener:controller.abort();
Removes event handlers that were attached with .on()
.
$('selector').off(eventName, handler)
Name | Type | Description |
---|---|---|
eventName | string | One or more space-separated event types |
handler | function | The function to remove from the event handlers |
Query object (for chaining).
function clickHandler(event) { console.log('Clicked!');}
$('button').on('click', clickHandler);// Later, to remove the event listener$('button').off('click', clickHandler);
Attaches a handler to an event for the elements. The handler is executed at most once per element per event type.
$('selector').one(eventName, handler)
Name | Type | Description |
---|---|---|
eventName | string | One or more space-separated event types |
handler | function | The function to execute when the event occurs |
Query object (for chaining).
$('button').one('click', function(event) { console.log('This will only fire once!');});
Executes all handlers attached to the matched elements for the given event type.
$('selector').trigger(eventName)
Name | Type | Description |
---|---|---|
eventName | string | The type of event to trigger |
Query object (for chaining).
$('button').trigger('click');
Creates and dispatches a custom event on the matched elements.
dispatchEvent
is integrated into component callbacks, see component events
$('selector').dispatchEvent(eventName, eventData, eventSettings)
Name | Type | Description |
---|---|---|
eventName | string | The name of the custom event to create |
eventData | object | Custom data to pass with the event |
eventSettings | object | Additional event settings |
Query object (for chaining).
const eventData = { delta: getResizeDelta() // pass along resize delta with event}$('.container').dispatchEvent('resize', eventData);
Gives focus to the first element in the set of matched elements.
$('selector').focus()
Query object (for chaining).
// Focus on an input element$('input.username').focus();
Removes focus from the first element in the set of matched elements.
$('selector').blur()
Query object (for chaining).
// Remove focus from whatever is focused$(document.activeElement).blur();