bigskysoftware / bigskysoftware/htmx

[Feature Request] Dedicated AJAX success event `htmx:responseSuccess`

Open
#3,198 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
49.4k
Forks
1.7k
Avg merge
3d 22h
Merged PRs (30d)
30

Description

### PROBLEM
If an error occurs during an ajax request, we can use `htmx:responseError` to handle it:
```html
hx-on:htmx:response-error="handleError()"
```

But there isn't a corresponding event for a success case. One could use this workaround:
```html
hx-on:htmx:after-request="if (event.detail.successful) handleSuccess()"
```

That's acceptable for trivial cases, but gets very messy and hard to maintain when more branching logic is required.

These are the related events, but they are for the "error" or "error-or-success" cases:
- [htmx:afterOnLoad](https://htmx.org/events/#htmx:afterOnLoad) This event is triggered after an AJAX onload has finished. Note that this does not mean that the content has been swapped or settled yet, only that the request has finished.
- [htmx:afterRequest](https://htmx.org/events/#htmx:afterRequest) This event is triggered after an AJAX request has finished either in the case of a successful request (although one that may have returned a remote error code such as a 404) or in a network error situation.
- [htmx:responseError](https://htmx.org/events/#htmx:responseError) This event is triggered when an HTTP error response occurs

### SOLUTION
It would be cleaner to have a "success case" event, e.g. `htmx:responseSuccess`.

That would be simpler and we could use different handlers for different responses, instead of one mega handler with ugly branching logic (or worse, doing it in a script file).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.