MDN self-hosted library · e01.org

HTMLInputElement: select() method

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<input type="text" id="text-box" size="20" value="Hello world!" />
<button>Select text</button>
js
function selectText() {
  const input = document.getElementById("text-box");
  input.focus();
  input.select();
}

document.querySelector("button").addEventListener("click", selectText);