[**@wocart/theme v4.1.0**](../../README)

***

[@wocart/theme](../../README) / [lifecycle](../README) / bindEach

# Function: bindEach()

> **bindEach**\<`T`\>(`selector`, `bind`, `options?`): () => `void`

Runs `bind` once on every element `selector` matches: when the page is
ready, after Astro's router swaps a page in, and after an editor's preview
swaps a section in. A bind may return a teardown, run when its element
leaves — before Astro swaps the page out, or when a later pass finds the
element gone. Returns a function that stops it and runs every teardown.

```ts
bindEach<HTMLFormElement>("[data-order-form]", (form) => {
  const onResize = () => layout(form);
  window.addEventListener("resize", onResize);
  return () => window.removeEventListener("resize", onResize);
});
```

## Type Parameters

### T

`T` *extends* `Element` = `HTMLElement`

## Parameters

### selector

`string`

### bind

(`element`) => [`Teardown`](../type-aliases/Teardown)

### options?

[`LifecycleOptions`](../interfaces/LifecycleOptions)

## Returns

() => `void`
