A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange below:

HTMLInputElement: setSelectionRange() method - Web APIs

HTMLInputElement: setSelectionRange() method

Baseline Widely available

The HTMLInputElement.setSelectionRange() method sets the start and end positions of the current text selection in an <input> or <textarea> element.

The element must be focused for the call to have any effect.

Optionally, you can specify the direction in which selection should be considered to have occurred. This lets you indicate, for example, that the selection was set by the user clicking and dragging from the end of the selected text toward the beginning.

This method updates the HTMLInputElement.selectionStart, HTMLInputElement.selectionEnd, and HTMLInputElement.selectionDirection properties in one call.

The element must be of one of the following input types: password, search, tel, text, or url. Otherwise the browser throws an InvalidStateError exception.

If you wish to select all text of an input element, you can use the HTMLInputElement.select() method instead.

Syntax
setSelectionRange(selectionStart, selectionEnd)
setSelectionRange(selectionStart, selectionEnd, selectionDirection)
Parameters
selectionStart

The 0-based index of the first selected character. An index greater than the length of the element's value is treated as pointing to the end of the value.

selectionEnd

The 0-based index of the character after the last selected character. An index greater than the length of the element's value is treated as pointing to the end of the value. If selectionEnd is less than selectionStart, then both are treated as the value of selectionEnd.

selectionDirection Optional

A string indicating the direction in which the selection is considered to have been performed. Possible values:

Return value

None (undefined).

Exceptions
InvalidStateError DOMException

Thrown if the element is not one of the following input types: password, search, tel, text, or url.

Examples

Click the button in this example to select the third, fourth, and fifth characters in the text box ("zil" in the word "Mozilla").

HTML
<input type="text" id="text-box" size="20" value="Mozilla" />
<button>Select text</button>
JavaScript
function selectText() {
  const input = document.getElementById("text-box");
  input.focus();
  input.setSelectionRange(2, 5);
}

document.querySelector("button").addEventListener("click", selectText);
Result Specifications Browser compatibility See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4