
Enter a search term above to see results...
Enter a search term above to see results...
Iterator methods in Query allow you to loop through, manipulate, and filter sets of elements efficiently.
Iterates over a Query object, executing a function for each matched element.
$('selector').each(function(element, index) { // Your code here})
Name | Type | Description |
---|---|---|
function | Function | Function to execute for each element |
Query object (for chaining).
$('div').each(function(el, index) { console.log(`Div ${index}:`, el.textContent);});
Execution Context Inside the callback,
this
refers to the current DOM element.
Creates a new array with the results of calling a provided function on every element in the set.
$('selector').map(function(element, index) { // Your code here return someValue;})
Name | Type | Description |
---|---|---|
function | Function | Function to execute for each element |
A new array with the results of the function calls.
const textContents = $('p').map(function(el, index) { return el.textContent;});console.log(textContents);
Return Values The
map()
method collects the return value of your callback function for each element into a new array.
Creates a new Query object with all elements that pass the test implemented by the provided function.
$('selector').filter(function(element, index) { // Your code here return true; // or false})
Name | Type | Description |
---|---|---|
function | Function or String | Function or selector to test elements |
A new Query object with the filtered elements.
const visibleElements = $('div').filter(function(el) { return $(el).css('display') !== 'none';});
Flexible Filtering You can pass either a function or a selector string to
filter()
. When using a function, returntrue
to include the element,false
to exclude it.