Drag to highlight text in the field, or use Select all. Status shows the selection.
action
Works on text-like <input> and <textarea>. Not for <select> menus.
Demo
Waiting for select…
Code
HTML Examples
Short study snippets. Use Try It Yourself for a live editor. Prefer listeners in real apps.
Example 1 — Inline onselect on an input
Recognize the attribute form. Highlight any part of the value.
HTML
<label for="phrase">Phrase</label>
<input
id="phrase"
type="text"
value="Hello CodeToFun"
onselect="document.getElementById('status').textContent = 'Text selected.'"
>
<p id="status">Select some text in the field.</p>
onselect registers an inline handler on the input.
Drag (or Shift+arrows) to highlight text.
select fires when a selection is made.
Status confirms the event ran.
Example 2 — Preferred listener + selected text
Show exactly what was highlighted using selectionStart and selectionEnd.
HTML
<label for="phrase">Phrase</label>
<input id="phrase" type="text" value="Try selecting some text in this element.">
<p id="status">Waiting…</p>
<script>
var input = document.getElementById("phrase");
var status = document.getElementById("status");
input.addEventListener("select", function () {
var selected = input.value.substring(input.selectionStart, input.selectionEnd);
status.textContent = "You selected: " + selected;
});
</script>
Name clash with <select> is the most common beginner mix-up.
dropdown
Not for <select> menus
Picking an option uses change. onselect is text highlighting only.
types
Text-like inputs only
Works with types like text, search, url, tel, password, and textarea. Not useful on checkbox / radio.
empty
Collapsed selection
If start equals end, nothing meaningful is highlighted. Check that before using the substring.
vs input
Different from typing events
input fires as values change. select fires when a highlight is created.
style
Prefer listeners
Inline on* attributes mix concerns. Use addEventListener in production code.
Compatibility
Browser Support
onselect is an event handler for text selection on inputs and textareas. Prefer addEventListener in apps.
✓ Baseline · Widely available
onselect
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the select event on text controls.
98%Global support (approx.)
Google ChromeAll
Full support
Mozilla FirefoxAll
Full support
Apple SafariAll
Full support
Microsoft EdgeAll
Full support
OperaAll
Full support
Samsung InternetAll
Full support
Internet ExplorerAll*
Supported*
selectWidely supported
Bottom line: *Safe on modern browsers. Remember: text selection — not <select> dropdown change.
Remember
Key Takeaways
Role: run script when text is highlighted in a field.
Read selection:selectionStart / selectionEnd.
Targets: text-like input and textarea.
Not for <select> dropdowns.
One line:onselect fires when text is selected in an input/textarea — use selectionStart/End to read it.
Frequently Asked Questions
It is an event handler for the select event. That event fires when the user selects some text inside an <input> (text-like types) or a <textarea>.
Use selectionStart and selectionEnd on the element, then substring the value between those indexes.
No. The select event is about text selection in inputs/textareas. Choosing an option in a <select> element is a different control — use change for that.
Prefer element.addEventListener("select", handler). MDN discourages inline HTML event handler attributes.
Yes. Call input.select() to select all contents, or set selectionStart / selectionEnd for a range. That can also fire the select event.
Show a preview of the highlighted text, enable a “copy selection” action, or teach selection APIs in demos.