
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}`);
Get the closest positioned ancestor element.
Edge-Case Improvements
offsetParent()
handles some [edge cases}(https://issues.chromium.org/issues/41131675) that prevent the naive use ofel.offsetParent
$('selector').offsetParent(options)
Name | Type | Description |
---|---|---|
options | Object | Configuration options for the method |
Query object containing the offset parent element(s).
const offsetParent = $('.child-element').offsetParent();console.log('Offset parent:', offsetParent.get(0));
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);