The HTMLInputElement.select() method selects all the text in a <textarea> element or in an <input> element that includes a text field.

Syntax

element.select();

Example

Click the button in this example to select all the text in the <input> element.

HTML

<input type="text" id="text-box" size="20" value="Hello world!">
<button onclick="selectText()">Select text</button>

JavaScript

function selectText() {
  const input = document.getElementById('text-box');
  input.focus();
  input.select();
}

Result

Notes

Calling element.select() will not necessarily focus the input, so it is often used with HTMLElement.focus().

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'select' in that specification.
Living Standard  

Browser compatibility

No compatibility data found. Please contribute data for "api.HTMLInputElement.select" (depth: 1) to the MDN compatibility data repository.

See also

Document Tags and Contributors

Contributors to this page: mfluehr, fscholz, sideshowbarker, jakobc, kscarfone, Krinkle
Last updated by: mfluehr,