| 1 | --- |
| 2 | title: "hx-boost" |
| 3 | description: "Converts links and forms to AJAX" |
| 4 | --- |
| 5 | |
| 6 | The `hx-boost` attribute allows you to "boost" normal anchors and form tags to use AJAX instead. This |
| 7 | has the [nice fallback](https://en.wikipedia.org/wiki/Progressive_enhancement) that, if the user does not |
| 8 | have javascript enabled, the site will continue to work. |
| 9 | |
| 10 | ## Syntax |
| 11 | |
| 12 | For anchor tags, clicking on the anchor will issue a `GET` request to the url specified in the `href` and |
| 13 | will push the url so that a history entry is created. The target is the `<body>` tag, and the `outerSync` |
| 14 | swap strategy is used by default. All of these can be modified by using the appropriate attributes, except |
| 15 | the `click` trigger. |
| 16 | |
| 17 | For forms the request will be converted into a `GET` or `POST`, based on the method in the `method` attribute |
| 18 | and will be triggered by a `submit`. Again, the target will be the `body` of the page, and the `outerSync` |
| 19 | swap will be used. The url will _not_ be pushed, however, and no history entry will be created. (You can use the |
| 20 | [`hx-push-url`](/reference/attributes/hx-push-url) attribute if you want the url to be pushed.) |
| 21 | |
| 22 | ```html |
| 23 | <div hx-boost:inherited="true"> |
| 24 | <a href="/page1">Go To Page 1</a> |
| 25 | <a href="/page2">Go To Page 2</a> |
| 26 | </div> |
| 27 | ``` |
| 28 | |
| 29 | These links will issue an ajax `GET` request to the respective URLs and swap the full response into the |
| 30 | `<body>` using `outerSync` — which replaces the body's children and syncs any attributes on the `<body>` |
| 31 | element itself (e.g. `class`, `data-*`) without removing and re-creating the node. |
| 32 | |
| 33 | ```html |
| 34 | <form hx-boost="true" action="/example" method="post"> |
| 35 | <input name="email" type="email" placeholder="Enter email..."> |
| 36 | <button>Submit</button> |
| 37 | </form> |
| 38 | ``` |
| 39 | |
| 40 | This form will issue an ajax `POST` to the given URL and swap the full response into the `<body>`. |
| 41 | |
| 42 | ## Advanced Syntax |
| 43 | |
| 44 | You can configure boost behavior using a config string that sets swap, target, and other options directly |
| 45 | on the `hx-boost` attribute. Use `hx-boost:inherited` to pass that config down to all boosted descendants. |
| 46 | |
| 47 | ```html |
| 48 | <!-- outerSync + select + target: pull #main from the full-page response and swap it in place, |
| 49 | syncing #main's attributes (class, data-*, etc.) at the same time --> |
| 50 | <body hx-boost:inherited="swap:outerSync select:#main target:#main"> |
| 51 | <nav> |
| 52 | <!-- These links inherit the boost config above --> |
| 53 | <a href="/page1">Go To Page 1</a> |
| 54 | <a href="/page2">Go To Page 2</a> |
| 55 | |
| 56 | <!-- Nested override: descendants of this div use a different boost config --> |
| 57 | <div hx-boost:inherited="swap:'innerHTML strip' target:#sidebar"> |
| 58 | <a href="/sidebar">Sidebar</a> |
| 59 | |
| 60 | <!-- Per-element override: this link uses its own config, ignoring all inherited --> |
| 61 | <a href="/modal" hx-boost="swap:beforeend target:#modals">Open Modal</a> |
| 62 | |
| 63 | <!-- Disable boost for this link --> |
| 64 | <a href="/external" hx-boost="false">External</a> |
| 65 | </div> |
| 66 | </nav> |
| 67 | |
| 68 | <!-- Non-boosted htmx elements are unaffected by hx-boost:inherited --> |
| 69 | <div hx-get="/data" hx-trigger="load">Loading...</div> |
| 70 | |
| 71 | <div id="main"></div> |
| 72 | <div id="sidebar"></div> |
| 73 | <div id="modals"></div> |
| 74 | </body> |
| 75 | ``` |
| 76 | |
| 77 | The key advantage is that boost config only applies to boosted elements (links and forms), unlike inherited |
| 78 | `hx-*` attributes which would affect all descendant htmx elements. |
| 79 | |
| 80 | ### Priority Order |
| 81 | |
| 82 | Boost config **overrides** explicit `hx-*` attributes on the element: |
| 83 | |
| 84 | 1. Boost config (highest priority) |
| 85 | 2. Explicit `hx-*` attributes |
| 86 | 3. Default values (lowest priority) |
| 87 | |
| 88 | This allows you to: |
| 89 | - Set base defaults with `hx-target`, `hx-swap` on elements |
| 90 | - Override them with `hx-boost:inherited` at any level |
| 91 | - Use `hx-boost="true"` or `hx-boost="false"` to enable/disable |
| 92 | |
| 93 | Supported modifiers: |
| 94 | - `swap:STYLE` - Swap strategy (`outerSync`, `innerHTML`, `outerHTML`, etc.) |
| 95 | - `target:SELECTOR` - Target element selector |
| 96 | - `select:SELECTOR` - Content selection from response (works with `outerSync`; use `innerHTML strip` instead of `innerHTML` + `select` when targeting a non-body element) |
| 97 | |
| 98 | ## Anchor Boosting |
| 99 | |
| 100 | Anchors are boosted when they navigate to the same origin in the current window: |
| 101 | |
| 102 | ```html |
| 103 | <a href="/example">Same-origin link</a> |
| 104 | <a href="/example" target="_self">Same-origin link targeting this window</a> |
| 105 | ``` |
| 106 | |
| 107 | These anchors are not boosted: |
| 108 | |
| 109 | ```html |
| 110 | <a href="https://other-domain.com/example">Different origin</a> |
| 111 | <a href="#section">Local anchor</a> |
| 112 | <a href="/report.pdf" download>Browser download</a> |
| 113 | <a href="/example" target="_blank">New window or tab</a> |
| 114 | ``` |
| 115 | |
| 116 | ## Notes |
| 117 | |
| 118 | * All requests are done via AJAX, so keep that in mind when doing things like redirects |
| 119 | * To find out if the request results from a boosted anchor or form, look for |
| 120 | [`HX-Boosted`](/reference/headers/HX-Boosted) in the request header |
| 121 | * Selectively disable boost on child elements with `hx-boost="false"` |
| 122 | * Disable the replacement of elements via boost, and their children, with |
| 123 | [`hx-preserve="true"`](/reference/attributes/hx-preserve) |