Enter a search term above to see results...
On This Page
Basic Usage
Constructor
The Query constructor is the foundation of the library, allowing you to create Query objects from various types of inputs.
Web Component Support - Semantic exports two separate helpers for handling DOM
$
and Shadow DOM$$
that will recursively cross shadow boundaries.
Arguments
Name | Type | Description |
---|---|---|
selector | string | Element | NodeList | Array<Element> | Window | The selector or elements to query |
options | object | Configuration options |
Options
Name | Type | Default | Description |
---|---|---|---|
root | Element | document | The root element to start the query from |
pierceShadow | boolean | false | Whether to pierce through Shadow DOM boundaries |
DOM Examples
DOM Selectors
You can pass in a string selector to select the matching elements in the page.
Note: Selecting elements using Query performs identically to
querySelectorAll
.
HTML Strings
You can pass through html directly into $
to create new nodes.
DOM Element
NodeList or HTMLCollection
Element Arrays
DocumentFragment
Globals
Query Object
Memory Usage - When creating a Query object for
window
orglobalThis
, Query uses a proxy object to avoid keeping a full copy of the global object in memory. This optimization helps reduce the memory footprint of your application.
Shadow DOM Examples
Query has special support for piercing shadow DOM boundaries using $$
or pierceShadow
. This can allow you to access content that might be difficult to access normally using standard DOM APIs.
Querying Shadow DOM
Selecting Slotted Content
The Query Object
When you use the $()
or $$()
function, it returns a Query object. This object contains a collection of DOM elements that have been selected from the document or created from an HTML string.
Properties of a Query Object
- length: The number of elements in the Query object.
- selector: The original selector used to create the Query object (if applicable).
- options: The options used when creating the Query object.
Chaining
Query methods can be chained for concise and readable code:
Iteration
Use the each
method to iterate over selected elements:
For more information see the dedicated page for utilities
Manipulation
You can use dedicated helpers like filter
and map
to manipulate Query content.
Map
Filter
Accessing Elements
You can access a single DOM element using the el
helper.
Single Element
You can access a single element using el
Single Index
To get a particular index you can use get
Empty Query Objects
A Query object may be empty if no elements match the selector or if all elements have been filtered out by chained methods. This allows chaining to continue unabated even with no results.
Differences from Native Arrays
The Query Object is array-like meaning you can also access elements idiosyncratically using bracket notation $('div')[0]
, however this is not recommended, as many other array functions like push
or pop
are not supported
Browser Helpers
A few additional helpers are provided specifically for using Query in browser environments.
Export Globals
If you need to restore the previous values before calling export globals.
Restore Globals
If you need to restore the previous values before calling export globals.
Custom Aliases
You can also choose to use Query as a custom alias
Custom Root
You can use the root
option to change the root which is used for query. This can be useful when using Query
in custom implementations scoped to an element.
SUI Components - Semantic UI components use this technique when returning
$
and$$
inside components