best way to trigger JS to focus an input text field that was newly swapped in
See original GitHub issuethis seems to be a common use case.
so I have a section of the page swapped with multiple elements that can include multiple <input> fields
but I want to focus only one of these fields, i.e. the cursor should be within a specified text field, and positioned after any text value that may be present
I’m finding it hard to figure out the best way to achieve this, I know afterSettle may be involved: https://htmx.org/events/#htmx:afterSettle
do suggest what you think would be the best / most elegant way to handle this
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Set the focus to HTML form element using JavaScript
Example 1: The focus() method is set to the input tag when user clicks on Focus button.
Read more >How do you automatically set the focus to a textbox when a ...
To use the HTML 5 attribute and fall back to a JS option: ... your page and reference a function the sets the...
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 >HTMLElement.focus() - Web APIs | MDN
Select the button to set focus on the text field. Focus on a button. This example demonstrates how you can set the focus...
Read more >Handling Events :: Eloquent JavaScript
To get the actual content that was typed, it is best to directly read it from the focused field. Chapter 18 will show...
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

Yes, it should. It there are any other nodes still in the DOM that have “autofocus”, they should have already gotten their chance to be focused. The pull request just scans the NEW content for the first “autofocus” attribute it can find. If there are any, then it calls
.focus()on it.Check it out, then nag @1cg to merge it 😉
HTMX has mechanisms for spinners when waiting for something to load over the network. Though it doesn’t have that for setting up a web socket connection, it could be added if necessary. You can set up a dynamic WebSocket/SSE declaratively when HTMX swaps in the partial markup sent back by the server. This part works great – although I think there’s still some work to be done in disconnecting a WebSocket/SSE when its root node is swapped out. I still need more research here.
I think this is my long-winded way of saying that I’d look into other ways to add “before” code into the call. That would mean that any javascript included along with a partial HTML could just run “after” it’s loaded, and we wouldn’t need the distinction, and we could keep the API as simple as possible.
For nasty errors and edge cases,
eval()potentially opens up security issues inside of HTMX. Although it’s a relatively low risk, I’d suggest we find some sort of “safe eval” to implement. I know (for example) Angular had some mechanism for this, so it should be simple to research.I agree with @ptrthomas, adding CSS sounds like a similar situation that we could explore here. From work in other environments, I think that browsers automatically use any CSS you dynamically add to the DOM, but the same is not true for Javascript. Either way, it would be nice to do some experiments and see how HTMX could make both of them “just work.”