
Enter a search term above to see results...
Enter a search term above to see results...
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.
Get the number of elements in the Query object.
Count Usage
count()
is preferred although the Query object isarray-like
andlength
will also work. If it’s really important to you — you can still do that.
$('selector').count()
number
- The number of elements in the Query object.
const paragraphCount = $('p').count();console.log(`There are ${paragraphCount} paragraphs on the page.`);
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.
$('selector').index()
number
- The index of the first element within the Query object relative to its sibling elements, or -1 if not found.
const index = $('li.active').index();console.log(`The active list item is at index ${index}`);
These methods provide access to individual elements in a Query collection.
Returns the first DOM element in the current set.
$('selector').el()
The first DOM element in the collection, or undefined
if the collection is empty.
const firstParagraph = $('p').el();console.log(firstParagraph.textContent);
Gets the DOM element at the specified index, or all elements if no index is provided.
$('selector').get(index)
Name | Type | Description |
---|---|---|
index | number | Optional index of the element to retrieve |
The element at the specified index, an array of all elements, or undefined
if the index is out of bounds.
// Get the second paragraph elementconst secondParagraph = $('p').get(1);
// Get all paragraph elements as an arrayconst allParagraphs = $('p').get();
Returns a new Query instance containing the element at the specified index.
$('selector').eq(index)
Name | Type | Description |
---|---|---|
index | number | Index of the element to retrieve |
A new Query instance containing the element at the specified index.
// Get the second paragraph as a Query objectconst secondParagraph = $('p').eq(1);secondParagraph.addClass('highlighted');
Returns a new Query instance containing the first element in the current set.
$('selector').first()
A new Query instance containing the first element in the collection.
// Get the first paragraph as a Query objectconst firstParagraph = $('p').first();firstParagraph.addClass('highlighted');
Returns a new Query instance containing the last element in the current set.
$('selector').last()
A new Query instance containing the last element in the collection.
// Get the last paragraph as a Query objectconst lastParagraph = $('p').last();lastParagraph.addClass('highlighted');
Gets or sets a property of elements in the current set.
// Get property$('selector').prop(name)
// Set property$('selector').prop(name, value)
Name | Type | Description |
---|---|---|
name | string | Name of the property to get or set |
value | mixed | Optional value to set for the property |
When getting: The property value of the first element. When setting: The Query instance for chaining.
// Get the checked state of a checkboxconst isChecked = $('input[type="checkbox"]').prop('checked');
// Set the disabled property on all buttons$('button').prop('disabled', true);
Reverses the order of the elements in the current set.
$('selector').reverse()
A new Query object containing the reversed elements.
// Reverse the order of list itemsconst $reversed = $('ul li').reverse();$('ul').empty().append($reversed);
Returns a shallow copy of a portion of the elements into a new Query object.
$('selector').slice(start, end)
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) |
A new Query object containing the sliced elements.
// 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);
// 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');
// Slice user posts in a user-profile componentconst $posts = $$('user-profile').find('.user-post');const $recentPosts = $posts.slice(0, 3);$recentPosts.addClass('recent');
Checks if there are any elements in the current Query object.
$('selector').exists()
boolean
- true
if the Query object contains at least one element, false
otherwise.
if ($('.my-element').exists()) { console.log('The element exists in the DOM');} else { console.log('The element does not exist in the DOM');}
Get or set component settings for all elements in the Query object.
// Get settings$('selector').settings()
// Set settings$('selector').settings(settings)
Name | Type | Description |
---|---|---|
settings | object | (Optional) Settings object to apply |
// Get component settingsconst settings = $('.my-component').settings();console.log(settings);
// Set component settings$('.my-component').settings({ duration: 300, easing: 'ease-in-out'});
Get or set a specific setting for all elements in the Query object.
// Get setting$('selector').setting(name)
// Set setting$('selector').setting(name, value)
Name | Type | Description |
---|---|---|
name | string | Name of the setting to get or set |
value | mixed | (Optional) Value to set |
// Get a specific settingconst duration = $('.my-component').setting('duration');console.log(duration);
// Set a specific setting$('.my-component').setting('duration', 500);
Initialize components on the selected elements.
$('selector').initialize()
Query object for chaining.
// Initialize all components$('.my-component').initialize();
// Initialize specific component types$('.modal').initialize();
Get component instances for the selected elements.
$('selector').component()
Component instance for the first element, or array of component instances for multiple elements.
// Get component instanceconst modalComponent = $('.modal').component();if (modalComponent) { modalComponent.show();}
// Get multiple component instancesconst tabComponents = $('.tab').component();tabComponents.forEach(tab => { tab.refresh();});
Get or set the data context for elements.
// Get data context$('selector').datacontext()
// Set data context$('selector').datacontext(data)
Name | Type | Description |
---|---|---|
data | object | (Optional) Data object to set |
// Get data contextconst data = $('.my-component').datacontext();console.log(data);
// Set data context$('.my-component').datacontext({ user: { name: 'John', id: 123 }, theme: 'dark'});
Export Query functions to the global scope.
$.exportGlobals()
Query object for chaining.
// Export $ and $$ to global scope$.exportGlobals();
// Now you can use $ and $$ without importing$('div').addClass('global-access');$$('my-component').css('color', 'red');