-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to keep focus in the input box that was just swapped #75
Comments
@kzbt I generally solve this by not replacing the input field and targetting another element instead. Is that an option for your use-case? |
I would like to add this behaviour to htmx, but right now it doesn't preserve focus. I think that a morphdom swap should do so, however: |
@bencroker I'm trying to achieve something like below, which as far as I could think of can only be accomplished by replacing the input itself (the red border on invalid input). I'm also sending a @chg20 I've tried it with morphdom and it doesn't seem to work with
I've also tried morphdom with |
Ok @kzbt, i see what you mean now. I was going to explore using |
OK, first attempt at an id based fix for this: |
The #id solution works! You can replace the whole parent-div and the #id will make sure to put focus on the field where it was. Here's a different solution similar to what @bencroker described -- "just replace the parts of the page you really need to replace, not the input field" Password: <input type="text" name="password" value=""
hx-trigger="keyup changed delay:1s"
hx-post="/register?validateField=password"
hx-swap="#password_error"
><br>
<div id="password_error"><!-- htmx placeholder --></div> you can see i have the imput and a "space" placeholder for the errors separately. So when I hit <div hx-swap-oob="#password_error"
id="password_error">
<span style="color:red">The password must be at least 6 characters.</span><br>
</div> The response tells HTMX
Sooo if the user keeps typing and there is no error Backend just responds with empty div like <div hx-swap-oob="#password_error"
id="password_error">
</div> so it replaces/removes the error. The user can keep typing since only the error |
I would like the input box be focused while doing inline validation with keyup trigger.
For example:
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.
However
load.htmx
did work.The text was updated successfully, but these errors were encountered: