| 1 | --- |
| 2 | title: "htmx.onLoad()" |
| 3 | description: "Execute callback when elements are loaded" |
| 4 | --- |
| 5 | |
| 6 | The `htmx.onLoad()` function registers a callback to execute when new content is processed by htmx. |
| 7 | |
| 8 | ## Syntax |
| 9 | |
| 10 | ```javascript |
| 11 | htmx.onLoad(callback) |
| 12 | ``` |
| 13 | |
| 14 | ## Parameters |
| 15 | |
| 16 | - `callback` - Function to call with the loaded element |
| 17 | |
| 18 | ## Example |
| 19 | |
| 20 | ```javascript |
| 21 | htmx.onLoad((element) => { |
| 22 | console.log('New content loaded:', element); |
| 23 | // Initialize widgets, attach listeners, etc. |
| 24 | }); |
| 25 | ``` |
| 26 | |
| 27 | This is useful for initializing JavaScript widgets or attaching event listeners to dynamically loaded content. |