Enter a search term above to see results...
On This Page
Query - Utilities
Utility methods in Query allow you to get information about elements, access specific elements in a collection, and work with element dimensions and positioning in the DOM.
count
Get the number of elements in the Query object.
Count Usage
count()is preferred although the Query object isarray-likeandlengthwill also work. If it’s really important to you — you can still do that.
Syntax
$('selector').count()Returns
number - The number of elements in the Query object.
Usage
const paragraphCount = $('p').count();console.log(`There are ${paragraphCount} paragraphs on the page.`);Example
index
Get the index of an element in the set of matched elements.
DOM Position
index()is useful for determining the position of an element among its siblings.
Syntax
$('selector').index()Returns
number - The index of the first element within the Query object relative to its sibling elements, or -1 if not found.
Usage
const index = $('li.active').index();console.log(`The active list item is at index ${index}`);Element Access Methods
These methods provide access to individual elements in a Query collection.
el
Returns the first DOM element in the current set.
Syntax
$('selector').el()Returns
The first DOM element in the collection, or undefined if the collection is empty.
Usage
const firstParagraph = $('p').el();console.log(firstParagraph.textContent);Example
get
Gets the DOM element at the specified index, or all elements if no index is provided.
Syntax
$('selector').get(index)Parameters
| Name | Type | Description |
|---|---|---|
| index | number | Optional index of the element to retrieve |
Returns
The element at the specified index, an array of all elements, or undefined if the index is out of bounds.
Usage
// Get the second paragraph elementconst secondParagraph = $('p').get(1);
// Get all paragraph elements as an arrayconst allParagraphs = $('p').get();eq
Returns a new Query instance containing the element at the specified index.
Syntax
$('selector').eq(index)Parameters
| Name | Type | Description |
|---|---|---|
| index | number | Index of the element to retrieve |
Returns
A new Query instance containing the element at the specified index.
Usage
// Get the second paragraph as a Query objectconst secondParagraph = $('p').eq(1);secondParagraph.addClass('highlighted');first
Returns a new Query instance containing the first element in the current set.
Syntax
$('selector').first()Returns
A new Query instance containing the first element in the collection.
Usage
// Get the first paragraph as a Query objectconst firstParagraph = $('p').first();firstParagraph.addClass('highlighted');last
Returns a new Query instance containing the last element in the current set.
Syntax
$('selector').last()Returns
A new Query instance containing the last element in the collection.
Usage
// Get the last paragraph as a Query objectconst lastParagraph = $('p').last();lastParagraph.addClass('highlighted');prop
Gets or sets a property of elements in the current set.
Syntax
// Get property$('selector').prop(name)
// Set property$('selector').prop(name, value)Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Name of the property to get or set |
| value | mixed | Optional value to set for the property |
Returns
When getting: The property value of the first element. When setting: The Query instance for chaining.
Usage
// Get the checked state of a checkboxconst isChecked = $('input[type="checkbox"]').prop('checked');
// Set the disabled property on all buttons$('button').prop('disabled', true);reverse
Reverses the order of the elements in the current set.
Syntax
$('selector').reverse()Returns
A new Query object containing the reversed elements.
Usage
// Reverse the order of list itemsconst $reversed = $('ul li').reverse();$('ul').empty().append($reversed);slice
Returns a shallow copy of a portion of the elements into a new Query object.
Syntax
$('selector').slice(start, end)Parameters
| Name | Type | Description |
|---|---|---|
| start | number | The beginning index of the specified portion of the collection (optional) |
| end | number | The end index of the specified portion of the collection (optional, exclusive) |
Returns
A new Query object containing the sliced elements.
Usage
Basic Slicing
// Get elements 2-4 from a list of itemsconst $items = $('.item');const $sliced = $items.slice(1, 4); // Gets items at index 1, 2, 3
// Get all elements from index 2 onwardsconst $fromIndex = $items.slice(2);
// Get the last 2 elements using negative indicesconst $lastTwo = $items.slice(-2);Working with Multiple Elements
// Slice and manipulate a portion of elements$('.card').slice(0, 3).addClass('featured');
// Process elements in batchesconst $allItems = $('.item');const firstBatch = $allItems.slice(0, 5);const secondBatch = $allItems.slice(5, 10);
firstBatch.addClass('batch-one');secondBatch.addClass('batch-two');Shadow DOM
// Slice user posts in a user-profile componentconst $posts = $$('user-profile').find('.user-post');const $recentPosts = $posts.slice(0, 3);$recentPosts.addClass('recent');Example
add
Creates a new Query collection combining the current elements with elements from the provided selector.
Syntax
$('selector').add(selector)Parameters
| Name | Type | Description |
|---|---|---|
| selector | string, Element, Element[], NodeList, HTMLCollection, Query | The selector or elements to add to the collection |
Returns
Query object containing the combined elements with duplicates removed.
Usage
Combine Different Element Types
// Combine headers and paragraphs into one collectionconst $content = $('h1, h2, h3').add('p');$content.addClass('styled-content');Add Specific Elements
// Add a specific element to an existing collectionconst $items = $('.list-item');const $withSpecial = $items.add(document.getElementById('special-item'));Progressive Collection Building
// Build collections step by steplet $collection = $('.initial');$collection = $collection.add('.more-items');$collection = $collection.add('.final-items');Notes
- Duplicates are automatically removed
- Returns the original instance if no new elements are found
- Preserves the original Query instance’s options (root, pierceShadow)
- Perfect for applying operations to elements from multiple selectors
Example
exists
Checks if there are any elements in the current Query object.
Syntax
$('selector').exists()Returns
boolean - true if the Query object contains at least one element, false otherwise.
Usage
if ($('.my-element').exists()) { console.log('The element exists in the DOM');} else { console.log('The element does not exist in the DOM');}Example
settings
Get or set component settings for all elements in the Query object.
Syntax
// Get settings$('selector').settings()
// Set settings$('selector').settings(settings)Parameters
| Name | Type | Description |
|---|---|---|
| settings | object | (Optional) Settings object to apply |
Returns
- Get: Settings object for the first element
- Set: Query object for chaining
Usage
// Get component settingsconst settings = $('.my-component').settings();console.log(settings);
// Set component settings$('.my-component').settings({ duration: 300, easing: 'ease-in-out'});Example
setting
Get or set a specific setting for all elements in the Query object.
Syntax
// Get setting$('selector').setting(name)
// Set setting$('selector').setting(name, value)Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Name of the setting to get or set |
| value | mixed | (Optional) Value to set |
Returns
- Get: Setting value for the first element
- Set: Query object for chaining
Usage
// Get a specific settingconst duration = $('.my-component').setting('duration');console.log(duration);
// Set a specific setting$('.my-component').setting('duration', 500);Example
initialize
Initialize components on the selected elements.
Syntax
$('selector').initialize()Returns
Query object for chaining.
Usage
// Initialize all components$('.my-component').initialize();
// Initialize specific component types$('.modal').initialize();Example
component
Get component instances for the selected elements.
Syntax
$('selector').component()Returns
Component instance for the first element, or array of component instances for multiple elements.
Usage
// Get component instanceconst modalComponent = $('.modal').component();if (modalComponent) { modalComponent.show();}
// Get multiple component instancesconst tabComponents = $('.tab').component();tabComponents.forEach(tab => { tab.refresh();});Example
datacontext
Get or set the data context for elements.
Syntax
// Get data context$('selector').datacontext()
// Set data context$('selector').datacontext(data)Parameters
| Name | Type | Description |
|---|---|---|
| data | object | (Optional) Data object to set |
Returns
- Get: Data context for the first element
- Set: Query object for chaining
Usage
// Get data contextconst data = $('.my-component').datacontext();console.log(data);
// Set data context$('.my-component').datacontext({ user: { name: 'John', id: 123 }, theme: 'dark'});Example
exportGlobals
Export Query functions to the global scope.
Syntax
$.exportGlobals()Returns
Query object for chaining.
Usage
// Export $ and $$ to global scope$.exportGlobals();
// Now you can use $ and $$ without importing$('div').addClass('global-access');$$('my-component').css('color', 'red');