HTMLInputElement: select() method

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("HTML DOM")}}

The **select()** method of the {{domxref("HTMLInputElement")}} interface selects all the text in an {{HTMLElement("input")}} element that includes a text field.

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

Syntax

js-nolint
select()

Parameters

None.

Return value

None ({{jsxref("undefined")}}).

Examples

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

HTML

html
<input type="text" id="text-box" size="20" value="Hello world!" />
<button>Select text</button>

JavaScript

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

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

Result

{{EmbedLiveSample("Examples")}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("HTMLInputElement")}}
  • {{domxref("HTMLInputElement.setSelectionRange()")}}