Best way to keep focus in the input box that was just swapped
See original GitHub issueI would like the input box be focused while doing inline validation with keyup trigger.
For example:
<input
class="input is-danger"
name="name"
type="text"
value="Invalid value"
hx-trigger="keyup changed delay:300ms"
hx-target="this"
hx-swap="outerHTML"
hx-post="/validate"
hx-params="name">
This works and the dom gets swapped if the user has not entered anything in 300ms. But once its swapped the input focus is lost, so the user cant keep editing to correct the mistake.
I’ve tried using the event system, but it seems like some of the event listeners are not getting registered.
htmx.on("afterSettle.htmx", function(e) {
console.log("This did not print");
});
However load.htmx did work.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to always focus keep focus in text box - Stack Overflow
What is the best way to always keep focus in the text box? Because the operator will be using a barcode scanner, I'd...
Read more >Focusing: focus/blur - The Modern JavaScript Tutorial
The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an...
Read more >The :focus-visible Trick | CSS-Tricks
Take two minutes right now and visit your current project in a browser. Then, using only the Tab key, you should be able...
Read more >focus-visible - CSS: Cascading Style Sheets - MDN Web Docs
This selector is useful to provide a different focus indicator based on the user's input modality (mouse vs. keyboard).
Read more >onfocus Event - W3Schools
Example. Execute a JavaScript when an input field gets focus: · input type="text" onfocus ; In HTML: · element onfocus ; Example. Using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

OK, first attempt at an id based fix for this:
https://github.com/bigskysoftware/htmx/commit/2ac221bdbc1f8ae22b10b36d3ad7666f07c663b4
Ok @kzbt, i see what you mean now. I was going to explore using
afterSwapto reset the focus, but there is a bug in which that event is not called https://github.com/bigskysoftware/htmx/issues/79#issuecomment-639399013.