htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Option to specify "swap" value when using htmx.ajax()

Open rogue26 opened this issue 3 years ago • 5 comments

As far as I can tell, it's not currently possible to specify an equivalent to hx-swap when using the htmx.ajax() javascript API documented here.. It's got target, values, etc. but no swap.

My current workaround is to specify the default swap style with a meta tag, but it would be nice to have the flexibility within the API. If this is already possible, I'd love to know how to do it.

PS - Thank you for creating HTMX.

rogue26 avatar Feb 03 '22 06:02 rogue26

I don't know if the doc has been updated since your message, but there is an example with swap.

htmx.ajax('GET', '/example', {target:'#myDiv', swap:'outerHTML'})

aymericderbois avatar Feb 24 '22 13:02 aymericderbois

I recently tried to use htmx.ajax as well and used the swap key as indicated in the documentation, with a value of "none" since I didn't want any swapping to happen, but it didn't work.

Workaround I found was to return a 204 - No Content response in my Django view : return HttpResponse(status=HTTPStatus.NO_CONTENT) (as explained here).

ThibH avatar Apr 07 '22 08:04 ThibH

Hi @rogue26 ,

It seems #769 fixed your issue the day after you posted it. Can you make sure it works as expected with htmx 1.7.x?

David-Guillot avatar Apr 20 '22 06:04 David-Guillot

@ThibH can you clarify what you expected? You wanted to return html from the server and have htmx throw it away vs use it?

Why use htmx's ajax over fetch in that case?

gone avatar Apr 22 '22 17:04 gone

@gone Yes that's what I wanted and to answer your question it really is just because I am already using htmx for everything so I got really used to it. I might as well use fetch for sure, but if it can be done with htmx, I find it easier this way :)

ThibH avatar May 06 '22 13:05 ThibH