Note

This post is a thought. It's a short note that I make about someone else's content online. Learn more about the process thoughts

Here's my thought on πŸ’­ </> htmx ~ The response-targets Extension


The htmx response-targets extension allows me to respond to errors from the backend and do normal htmx swaps.

Note

by default htmx will only swap on 200 and 300 responses

Load the extension in head


<script src="https://unpkg.com/[email protected]/dist/ext/response-targets.js"></script>

Use the extension on an endpoint that might return a 400.


<div hx-ext="response-targets">
    <div id="response-div"></div>
    <button hx-post="/register"
            hx-target="#response-div"
            hx-target-5*="#serious-errors"
            hx-target-404="#not-found">
        Register!
    </button>
    <div id="serious-errors"></div>
    <div id="not-found"></div>
</div>

This post was a thought by Waylon Walker see all my thoughts at https://waylonwalker.com/thoughts