onselect Event
Example
Execute a JavaScript when some text has been selected:
<input type="text" onselect="myFunction()">
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The onselect event occurs after some text has been selected in an element.
The onselect event is mostly used on <input type="text"> or <textarea> elements.
Browser Support
Event | |||||
---|---|---|---|---|---|
onselect | Yes | Yes | Yes | Yes | Yes |
Syntax
In JavaScript, using the addEventListener() method:
object.addEventListener("select", myScript);
Try it Yourself »
Technical Details
Bubbles: | No |
---|---|
Cancelable: | No |
Event type: | UiEvent if generated from a user interface, Event otherwise |
Supported HTML tags: | <input type="file">, <input type="password">, <input type="text">, and <textarea> |
DOM Version: | Level 2 Events |
More Examples
Example
Using the select() method of the HTML DOM Input Text Object to select some content of a text field. When this happens, the onselect event fires, which will trigger an alert function.
// Select the contents of a text field
function mySelectFunction() {
document.getElementById("myText").select();
}
// Alert some text when the text in the text field has been selected
function myAlertFunction() {
alert("You selected some text!");
}
Try it Yourself »