_reswap(), _reselect() htmx

Let the server decide how (HX-Reswap) and which part (HX-Reselect) of its answer htmx swaps, over hx-swap / hx-select of the request.

_class('htmx')->_reswap($swap)
_class('htmx')->_reselect($selector)

Options

OptionMeaning
_reswap($swap)innerHTML, outerHTML, beforeend, none... (line breaks removed); the response goes on.
_reselect($selector)CSS selector of the part to swap in; the response goes on.

Example

First row

Code

$hx = _class('htmx');
if ($hx->_wants_fragment() && ($_GET['hx'] ?? '') === 'append') {
    $hx->_reswap('beforeend');
    $hx->_reselect('.docs-ref-hx-pick');
    return $hx->_result('<p>skipped by HX-Reselect</p><div class="docs-ref-hx-pick">Row added at ' . date('H:i:s') . '</div>');
}
asset('htmx');
return '<div id="docs-ref-hx-rows" class="well"><div>First row</div></div>'
    . '<button type="button" class="btn btn-primary" hx-get="' . _prepare_html(url('/@object/htmx/reswap')) . '" hx-vals=\'{"hx": "append"}\' hx-target="#docs-ref-hx-rows" hx-swap="innerHTML">Add a row</button>';

Notes

  • The button asks for innerHTML; the server answers beforeend, so rows are appended, and only .docs-ref-hx-pick of the answer is used.
  • The admin confirm uses both: innerHTML keeps the target and its id, .yf-admin-confirm picks the form (admin confirm).

See also: htmx/fragment, htmx/admin_confirm