files/en-us/web/api/htmlinputelement/setrangetext/index.md
{{APIRef("HTML DOM")}}
The HTMLInputElement.setRangeText() method replaces a
range of text in an {{HTMLElement("input")}} or {{HTMLElement("textarea")}} element with
a new string.
setRangeText(replacement)
setRangeText(replacement, start)
setRangeText(replacement, start, end)
setRangeText(replacement, start, end, selectMode)
replacement
start {{optional_inline}}
selectionStart value (the start of the user's current selection).end {{optional_inline}}
selectionEnd value (the end of the user's current
selection).selectMode {{optional_inline}}
"select" selects the newly inserted text."start" moves the selection to just before the inserted text."end" moves the selection to just after the inserted text."preserve" attempts to preserve the selection. This is the default.None ({{jsxref("undefined")}}).
Click the button in this example to replace part of the text in the text box. The newly inserted text will be highlighted (selected) afterwards.
<input
type="text"
id="text-box"
size="30"
value="This text has NOT been updated." />
<button>Update text</button>
function selectText() {
const input = document.getElementById("text-box");
input.focus();
input.setRangeText("ALREADY", 14, 17, "select");
}
document.querySelector("button").addEventListener("click", selectText);
{{EmbedLiveSample("Examples")}}
{{Specifications}}
{{Compat}}