htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Have a callback function parameter for requests

Open iustin94 opened this issue 2 years ago • 2 comments

I looked into the documentation and couldn't find an easy way to do this.

It could be usefull to have a callback function parameter on requests that could handle simple things. The use case I have in mind is that when an hx-post request is made, it might return some data on success. In my case, I want that data to be shown as a notification to the user which I do by calling a notification(msg) function.

The way to achieve this at the moment seems to be, that I have to create a custom event handler and inspect the event source, then I can do my logic. For a simple usecase like this, I think it would be worth having a callback function parameter that htmx can take as input and call it after everything finishes with the request.

iustin94 avatar Dec 11 '23 12:12 iustin94

would something like hx-on not solve this?

you could have something like <button hx-post="/do-something" hx-on::before-swap="notification(event)">create message</button>

this should call notification with the event data when the response is received but before it's swapped into the DOM.

itepastra avatar Dec 25 '23 20:12 itepastra

I believe it might, I didn't find this when I read through the docs. I'll give it a shot next time 👍🏻

iustin94 avatar Jan 08 '24 07:01 iustin94