HTML onselect Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onselect attribute handles the select event. It fires when the user selects some text inside an <input> (text-like types) or a <textarea>.

Read the highlight with selectionStart / selectionEnd. Prefer addEventListener("select", …). Related field events: oninput, onchange, onfocus.

Baseline · Widely available.

Stable across modern browsers since about July 2015. Not deprecated.

Not the <select> element.

This event is text selection in inputs/textareas — not choosing a dropdown option. For dropdowns, use change.

Inline handlers discouraged.

MDN: prefer addEventListener over inline on* attributes. Study onselect to recognize it in markup.

HTML Syntax

syntax.html
<input onselect=" script">

script runs when text is selected. Same idea on <textarea>. Preferred: element.addEventListener("select", handler).

Attribute Values

How you listen for text select:

Value / formDescriptionTry it
JavaScript on controlInline handler, e.g. showSel()Try it
addEventListener("select")Preferred; read selectionStart/EndTry it
select() APISelect all text from a buttonTry it

Live Preview

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…

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>
Try It Yourself

How It Works

  1. onselect registers an inline handler on the input.
  2. Drag (or Shift+arrows) to highlight text.
  3. select fires when a selection is made.
  4. 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>
Try It Yourself

How It Works

  1. Listener runs whenever text is selected.
  2. selectionStart / End mark the highlight range.
  3. substring extracts the selected characters.
  4. Status prints that substring (MDN-style logger).

Example 3 — select() on a textarea

Programmatically select all text; select still fires.

HTML
<label for="note">Note</label>
<textarea id="note" rows="3" cols="40">Select all of this note with the button.</textarea>
<p>
  <button type="button" id="all">Select all</button>
</p>
<p id="status" aria-live="polite">Waiting…</p>

<script>
  var note = document.getElementById("note");
  var status = document.getElementById("status");

  note.addEventListener("select", function () {
    var selected = note.value.substring(note.selectionStart, note.selectionEnd);
    status.textContent = "Selected (" + selected.length + " chars): " + selected;
  });

  document.getElementById("all").addEventListener("click", function () {
    note.focus();
    note.select();
  });
</script>
Try It Yourself

How It Works

  1. textarea supports the same select event.
  2. note.select() highlights the full value.
  3. select event runs and reports length + text.
  4. focus() first so the selection is visible.

Edge Cases & Pitfalls

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.

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 Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All*
Supported*
select Widely supported

Bottom line: *Safe on modern browsers. Remember: text selection — not <select> dropdown change.

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.

Next: onstalled

Detect when media data unexpectedly stops arriving.

onstalled tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful