hx-pod

{ "HX-Retarget": "#my-special-target" }


What is hx-pod?

Join me while I learn HTMX and talk at you about it
Twitter: @htmxlabs

Lazarus:

So our next response header here is hx retarget and this response target, this response header gets sent back from the server just like all the other response headers, in your response to a to an h t m x request. So that's what's going to handle it, fact that it is a response coming from an h t m x request. That allows you to add or override the h x target value. So, if there wasn't one on there before now you can add it, and if there was one there before you can replace it. So just a reminder, the h x target value and the value that you use for the h x retarget should be a CSS selector.

Lazarus:

And that's where you're going to place the HTML or the, you know, whatever is in your response in the DOM. So where are you going to put that into the DOM when the request gets back and your, you know, your request comes in, when your response goes back it has the data, Where is that going to go in the dom? That's what your hx target is originally setting and that's what your hx retarget response header is going to override. So, you know, normally like all these headers you're just, you're usually just using the attributes. You don't really need to use this hx retarget, response header.

Lazarus:

But when you have a special case and you need to change your h x target and your your code needs to handle it on the server side, you know, this allows you to retarget. So there's I could you know, there's maybe some some uses for this. Let's say like, one example could be an error. Maybe you have kind of a global error handler, or or like a permissions handler. So rather than putting it into your normal target of your h t m x, request.

Lazarus:

Maybe you have a special error box on your page that pops up or that is it down in the bottom right or something. And so when there's an error you could have something that just sends this h x retarget no matter what your previous h x, you know, whatever your your previous thing was, now you're gonna move it. So there's kind of a there's some interesting, you know, this is low level. So there's interesting stuff you can do with these response headers and just with the attributes in general, you know, you can send stuff all over your page, and we've seen that before with a bunch of these different things, the h x swap o o b. So so that's where the retarget comes in.

Lazarus:

It's it's just like a reswap and reselect, which we're gonna do next. But these headers all just override the matching attribute, that you originally can put into the front end. And it does make me wonder, is there this is kind of an aside, but is there a world where people are using these headers and almost nothing else? Like they're not setting all this stuff in the front end. There's sort of this, feels almost like a a secret layer of h t m x abilities.

Lazarus:

All these request headers and response headers, they can do a lot of stuff, they can put things around the page, and they're sort of invisible. And I'm trying to think what that would look like. What would that look like if you were to just kind of use these? And, you know, I'm not sure that it makes any sense. I'm just kind of thinking out loud right here, because these are short app.

Lazarus:

This is like the h x retarget doesn't do a ton. It just we've already talked about h x target, which allows you to, you know, put a CSS selector for where you want the data to go. This just lets you override that in the response headers. So I'm gonna look I'm gonna try to think a little bit about what these response headers, you know, is there a use for them besides just kind of on a case by case basis overriding the existing, attributes. And I'm not sure about that.

Lazarus:

If you have any ideas, please, reach out to me on Twitter, HTMX lab at HTMX Labs. So in the meantime, JET's just retarget lets you override your h x target value or add a new one from the response to at the to any h t m x request. So and you use it by just adding it, you know, whatever your back end system is they will allow you to add headers onto your response. So, in this case you just add the header hxdashretarget. H x is capital, so is the r in retarget.

Lazarus:

So I don't know if that's something that's required or not, but that's the that's the format. And then the value is a CSS selector of where you want to put the data into the dom. So, you know, you can put an ID, you can put, you know, closest. There's a couple of there's different for these CSS selectors. So that's h x retarget.

Lazarus:

Next, we're going to talk about h x re select and be very similar.