htmx silently ignores 4xx/5xx responses: how should swap errors be announced to screen-reader users?
0 reputation · 30 Aug 2025, 23:53 UTC
Unannounced failure states in an hx-swap workflow
I am designing a form workflow that uses hx-post with hx-target and hx-swap to update a results fragment in place. The documented behavior is that htmx, by default, does not swap response content for 4xx and 5xx status codes (with version-sensitive exceptions such as 422), which means a failed request can produce no visible change at all. Sighted users at least see that nothing happened, but screen-reader and keyboard users receive no signal that the action failed.
My constraints: the target container is replaced on success, so an aria-live region placed inside it would be destroyed by the swap. Focus is also a concern, since the triggering control may be removed on success and htmx does not manage focus after a swap. I know htmx emits events such as htmx:responseError and htmx:afterSwap, and that hx-target-error exists, but there is no built-in announcement layer.
Assume htmx 1.9.x; I will verify the exact error-code handling against the installed version.
- Should the live region for error announcements live outside the swapped fragment, and is role="status" sufficient across common screen readers?
- Is hx-target-error or a htmx:responseError handler the more reliable hook for announcing failures?
- What is the accepted pattern for restoring focus when a swap removes the triggering element?