On This Page
Methods
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
| Name | Type | Description |
|---|---|---|
| callback | function | Function 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
| Name | Type | Description |
|---|---|---|
| callback | function | Function 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
| Name | Type | Description |
|---|---|---|
| selector | string | CSS selector to match |
| fn | function | Test 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
| Name | Type | Description |
|---|---|---|
| index | number | Index of element to retrieve (supports negative) |
Returns
- With index: DOM element at the index, or
undefinedif 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));