← Back to Index

.each()

Iterate over an SR object, executing a function for each matched element.

Examples

1. Basic Iteration

// <li>A</li><li>B</li>
$('li').each((index, el) => {
     console.log(`${index}: ${el.textContent}`);
});

Result

// Console Output:
0: A
1: B

2. Breaking the loop

$('li').each(function(index) {
     if (index === 2) return false;
     $(this).addClass('processed');
});

Result

<li class="processed">Item 0</li>
<li class="processed">Item 1</li>
<li>Item 2</li>

Parameters

Parameter Type Description
callback function A function to execute for each matched element. The callback receives (index, element) arguments. Inside the callback, this refers to the raw DOM element. Return false to stop iteration early.

Returns: The original SR object for chaining.