const start = cursor.selectionStart; // in this case maybe 0 cursor.setRangeText(text); cursor.selectionStart = cursor.selectionEnd = start + text.length;