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.

Preserve selection after swap

See original GitHub issue

After 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
1cgcommented, Mar 21, 2021

Checking in on this. It should work, since my code is perfect in every way and never has any bugs no sir.

0reactions
dwasylcommented, Aug 6, 2022

@1cg Do you think this is at all connected to #595? I’ve noticed that if an input doesn’t have hx-preserve and has focused before a background refresh the same element will have focus in once the page was been “refreshed”. Currently, if the focused element has hx-preserve on 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

Read more comments on GitHub >

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

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