Query - Iterators API reference for Query iterator methods repeat API Reference
Categories

Query - Iterators

Loop through matched elements, transform values, and access underlying DOM elements.

Methods

each

$('selector').each(callback);

Executes a function for each matched element. Inside the callback, this refers to the current DOM element.

Parameters

NameTypeDescription
callbackfunctionFunction called with (element, index) for each element

Returns

Query object (for chaining).

Usage

$('p').each((el, index) => {
console.log(`Paragraph ${index}:`, el.textContent);
});

Example

map

$('selector').map(callback);

Creates a new array with the results of calling a function on every matched element.

Parameters

NameTypeDescription
callbackfunctionFunction called with (element, index), returns a value

Returns

Array of values returned by the callback.

Usage

const ids = $('button').map((el) => el.id);

Example

filter

$('selector').filter(selector);
$('selector').filter(fn);

Reduces the set to elements matching the selector or passing the function test.

See also filter in DOM Traversal for selector-based filtering patterns.

Parameters

NameTypeDescription
selectorstringCSS selector to match
fnfunctionTest function, return true to include

Returns

Query object containing filtered elements.

Usage

By Selector
$('p').filter('.highlight').css('background', 'yellow');
By Function
$('div').filter((el) => $(el).css('display') !== 'none');

Example

get

$('selector').get();
$('selector').get(index);

Retrieves DOM elements from the Query object. Returns native DOM elements, not Query objects.

Parameters

NameTypeDescription
indexnumberIndex of element to retrieve (supports negative)

Returns

  • With index: DOM element at the index, or undefined if out of range
  • Without index: Array of all DOM elements

Usage

Single Element
const first = $('p').get(0);
const last = $('p').get(-1);
All Elements
const elements = $('p').get();
elements.forEach(el => console.log(el.textContent));

Example

Previous
Events
Next
Logical Operators