onselect event

Definition and usage

The onselect event occurs after the text in the element is selected.

The onselect event is mainly used for <input type="text"> or <textarea> elements.

Instance

Example 1

Execute JavaScript when text is selected:

<input type="text" onselect="myFunction()">

Try it yourself

Example 2

Use the select() method of the HTML DOM Input Text object to select some content of the text field. When this happens, the onselect event is triggered, which triggers the alert function.

// Select the content of the text field
function mySelectFunction() {
  document.getElementById("myText").select();
}
// Alert some text when the text in the text field is selected
function myAlertFunction() {
  alert("You selected some text!");
}

Try it yourself

Syntax

In HTML:

<element onselect="myScript">

Try it yourself

In JavaScript:

object.onselect = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("select", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Not supported
Cancelable: Not supported
Event type: If generated from the user interfaceUiEvent. Otherwise Event.
Supported HTML tags: <input type="file">, <input type="password">, <input type="text"> as well as <textarea>
DOM Version: Level 2 Events

Browser Support

Events Chrome IE Firefox Safari Opera
onselect Support Support Support Support Support