Preserve selection after swap
See original GitHub issueAfter swapping the selection on the focussed input element gets lost.
Isolved the issue using this js Snippet… might be a good thing to have in the library natively. The element is selected by name or id. I know it’s a little hacky, but it’s sufficient to demonstrate the idea.
The interesting part is calling setSelectionRange.
(function() {
var activeId = null, activeName = null, selectionStart = 0, selectionEnd = 0;
function beforeInsert() {
activeId = null;
activeName = null;
selectionStart = 0;
selectionEnd = 0;
var activeElement = document.activeElement
if (activeElement) {
if (activeElement.id) {
activeId = activeElement.id;
} else if (document.activeElement.name) {
activeName = activeElement.name;
}
if (activeId || activeName) {
selectionStart = activeElement.selectionStart;
selectionEnd = activeElement.selectionEnd;
}
}
}
function afterInsert() {
var elem = null;
if (activeId) {
elem = document.getElementById(activeId);
} else if (activeName) {
elem = document.getElementsByName(activeName)[0];
}
if (elem) {
elem.focus();
if (selectionStart !== null && selectionEnd !== null) {
elem.setSelectionRange(selectionStart, selectionEnd);
}
}
}
if (document.addEventListener) {
document.addEventListener('htmx:beforeSwap', beforeInsert);
document.addEventListener('htmx:afterSwap', afterInsert);
}
})();
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Preserve JTable selection across TableModel change
You need to preserve the selection and then re-apply it. First of all you will need to get a list of all the...
Read more >Create and use variants - Figma Help Center
Figma will attempt to preserve your overrides when you select a different variant, or swap between instances in the Instance menu. Figma uses...
Read more >How to switch columns, rows, cells with Swap Ranges for Excel
Exchange ranges, preserve formatting, and choose how to process ... Click on the Select range icon in the Select range 1 field to...
Read more >How To Swap Cells in Excel (3 Methods With Tips) | Indeed.com
To do this, select a range of cells rather than a single cell at the point at which the process prompts you to...
Read more >8.14. Installation Destination Red Hat Enterprise Linux 7
This will generate a /boot partition, a / (root) volume, and a swap volume ... Click the Update Settings button to save your...
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

Checking in on this. It should work, since my code is perfect in every way and never has any bugs no sir.
@1cg Do you think this is at all connected to #595? I’ve noticed that if an
inputdoesn’t havehx-preserveand has focused before a background refresh the same element will have focus in once the page was been “refreshed”. Currently, if the focused element hashx-preserveon tit when the page is “refreshed” the focus is removed from any element. (I’ve run into this because user’s were complaining the refresh happens while they type and if they happen to it “space” before realizing the focus issue the browser can jump them to the very end up a very long page.Maybe this is different than this, but this seems to indicate it’s solved in circumstances without the
hx-preserve