---
title: "</> htmx ~ The response-targets Extension"
description: "!https://htmx.org/extensions/response-targets/"
date: 2024-04-30
published: true
tags:
  - dev
  - htmx
  - thought
  - webdev
template: link
---


<div class="embed-card embed-card-external">
  <a href="https://htmx.org/extensions/response-targets/" class="embed-card-link" target="_blank" rel="noopener noreferrer">
    <div class="embed-card-content">
      <div class="embed-card-title">&lt;/&gt; htmx ~ The htmx Response Targets Extension Extension</div>
      <div class="embed-card-description">htmx gives you access to AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes, so you can build modern user interfaces with the simplicity and power of hypert...</div>
      <div class="embed-card-meta">htmx.org</div>
    </div>
  </a>
</div>


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

``` html
<script src="https://unpkg.com/htmx.org@1.9.12/dist/ext/response-targets.js"></script>
```

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


``` html
<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>
```
