Query - Utilities API reference for Query methods related to element dimensions, positioning, and element access tool API Reference
Categories

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 is array-like and length will 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

NameTypeDescription
indexnumberOptional 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 element
const secondParagraph = $('p').get(1);
// Get all paragraph elements as an array
const allParagraphs = $('p').get();

eq

Returns a new Query instance containing the element at the specified index.

Syntax

$('selector').eq(index)

Parameters

NameTypeDescription
indexnumberIndex 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 object
const 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 object
const 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 object
const 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

NameTypeDescription
namestringName of the property to get or set
valuemixedOptional 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 checkbox
const 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 items
const $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

NameTypeDescription
startnumberThe beginning index of the specified portion of the collection (optional)
endnumberThe 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 items
const $items = $('.item');
const $sliced = $items.slice(1, 4); // Gets items at index 1, 2, 3
// Get all elements from index 2 onwards
const $fromIndex = $items.slice(2);
// Get the last 2 elements using negative indices
const $lastTwo = $items.slice(-2);

Working with Multiple Elements

// Slice and manipulate a portion of elements
$('.card').slice(0, 3).addClass('featured');
// Process elements in batches
const $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 component
const $posts = $$('user-profile').find('.user-post');
const $recentPosts = $posts.slice(0, 3);
$recentPosts.addClass('recent');

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

NameTypeDescription
settingsobject(Optional) Settings object to apply

Returns

  • Get: Settings object for the first element
  • Set: Query object for chaining

Usage

// Get component settings
const 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

NameTypeDescription
namestringName of the setting to get or set
valuemixed(Optional) Value to set

Returns

  • Get: Setting value for the first element
  • Set: Query object for chaining

Usage

// Get a specific setting
const 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 instance
const modalComponent = $('.modal').component();
if (modalComponent) {
modalComponent.show();
}
// Get multiple component instances
const 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

NameTypeDescription
dataobject(Optional) Data object to set

Returns

  • Get: Data context for the first element
  • Set: Query object for chaining

Usage

// Get data context
const 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');

Example

Previous
Logical Operators
Next
Internal