> For the complete documentation index, see [llms.txt](https://jscula.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jscula.gitbook.io/docs/manipulation/.foreach.md).

# .forEach()

With **Jscula**, you’ll **rarely need to manually loop through elements** just to apply actions.

Most DOM operations are already handled **gracefully and intelligently**.

***

### Syntax

```js
$(selector*).forEach(callback); //only on NodeList and HTMLCollections
```

> ⚠️ Note: `.forEach()` works only when the selector targets **multiple elements** (`*`).

***

### Callback Signature

```js
(element, index, list) => {}
```

* `element` → current DOM element
* `index` → zero-based position
* `elementList` - The selected Lists of elements  either NodeList or HTMLCollection

***

### Basic Example

```js
$('.card*').forEach((el, i) => {
  el.dataset.index = i;
});
```

***

### Conditional Logic Example

```js
$('input*').forEach(input => {
  if (input.val() === '') {
    input.addClass('error');
  }
});
```

***

### When Should You Use `.forEach()`?

Use `.forEach()` **only when you need to do something beyond a direct action**, such as:

* Conditional logic per element
* Reading values
* Custom computations
* Advanced DOM interaction

That’s where `.forEach()` comes in.

### Key Takeaway

> **Jscula minimizes the need for loops.**\
> If all you want is to apply an action — just use the API directly.\
> If you truly need to iterate — `.forEach()` is there.
