Execute a JavaScript when some text has been selected:
More "Try it Yourself" examples below.
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.
Event | |||||
---|---|---|---|---|---|
onselect | Yes | Yes | Yes | Yes | Yes |
In HTML:
In JavaScript:
In JavaScript, using the addEventListener() method:
Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.
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">, <keygen>, and <textarea> |
DOM Version: | Level 2 Events |
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.