question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to keep focus in the input box that was just swapped

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
1cgcommented, Jun 11, 2020
0reactions
bencrokercommented, Jun 5, 2020

Ok @kzbt, i see what you mean now. I was going to explore using afterSwap to reset the focus, but there is a bug in which that event is not called https://github.com/bigskysoftware/htmx/issues/79#issuecomment-639399013.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found