<input type="text" id="text-box" size="20" value="Hello world!" />
<button>Select text</button>HTMLInputElement: select() method / notebook
HTMLInputElement: select() 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.select();
}
document.querySelector("button").addEventListener("click", selectText);