<input
type="text"
id="text-box"
size="30"
value="This text has NOT been updated." />
<button>Update text</button>HTMLInputElement: setRangeText() method / notebook
HTMLInputElement: setRangeText() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
function selectText() {
const input = document.getElementById("text-box");
input.focus();
input.setRangeText("ALREADY", 14, 17, "select");
}
document.querySelector("button").addEventListener("click", selectText);