I want to replace a single attribute on a single DOM element using HTMX, something easy to do in JS but I can't seem to figure out the best way in HTMX to do it.
My specific use-case is an input field where I want to change the attribute aria-invalid without disturbing anything else.
- According to docs hx-preserve does what I want in general except for the specific case I want (preserver focus, caret, etc.). Suggests morphing.
- Morphing should work, but I have to include another script library (Idiomorph or morphdom-swap) or use v2.0 of Htmx (not released yet) to get it out of the box. Overcomplicated it seems to replace a single attribute.
- Use an event like htmx:afterSwap to run a small snippet of JS code to set the attribute (seems easiest but is this a bad idea?)
After trying many ways, here is the simplest I found so far. Maybe somebody else has a better way.
Note: I wanted to just do this from the input element, but I couldn't get hx-on:htmx:afterSwap to work at all. Event fires in debugger, but my code not executed for that event for some reason but it works for others. E.g. If I changed it to hx-on:htmx:afterRequest it works.