Res

by chintsu

HTML

<input id="input" type="text" value="Some long text"/>
<input id="btnRememberSelection" type="button" value="Remember Selection"/>
<input id="btnRestoreSelection"  type="button" value="Restore Selection"/>

JavaScript

const input = document.getElementById('input');
const btnRemember = document.getElementById('btnRememberSelection');
const btnRestore = document.getElementById('btnRestoreSelection');

const selection = {
	start: 0,
  end: 0,
  direction: 'none'
}

function remember() {
	input.focus();
	selection.start = input.selectionStart;
  selection.end = input.selectionEnd;
  selection.direction = input.selectionDirection;
}

function restore() {
	input.focus();
	input.setSelectionRange(selection.start, selection.end, selection.direction);
}

btnRemember.addEventListener('click', remember);
btnRestore.addEventListener('click', restore);