👀 Live Preview
Highlight text below — the selection count updates:
0 characters selected

The onselect attribute is an inline event handler that runs JavaScript when the select event fires — when the user highlights text inside an <input> or <textarea>. Use it to show selection counts, enable a “format selection” button, validate highlighted content, or provide contextual help. Read the selection with selectionStart, selectionEnd, and value.substring(). For selections anywhere on the page, use document.selectionchange or window.getSelection() instead. Prefer addEventListener("select", …) in production.
Inline JS.
Text highlight.
Form fields.
Read range.
Select vs copy.
Preferred way.
onselect AttributeThe primary purpose of onselect is to react when the user selects (highlights) text inside a form control — so your page can show how many characters are selected, reveal formatting tools, or run validation on the highlighted portion only.
The select event fires on the element where the selection occurs. It does not fire when the user copies text — use oncopy for clipboard actions — and it does not apply to arbitrary page content outside inputs and textareas.
onselect runs when text is highlighted. Use oncopy if you need to react when the user actually copies to the clipboard.
Set onselect on an <input> or <textarea>, or assign element.onselect in JavaScript:
<input type="text" onselect="handleSelection()">
<textarea onselect="handleSelection()"></textarea>
<script>
field.onselect = handleSelection;
field.addEventListener("select", handleSelection);
</script>select event fires.<input> types and <textarea>.selectionStart and selectionEnd.element.onselect in script.addEventListener("select", handler).document.addEventListener("selectionchange", …).The onselect attribute accepts a string of JavaScript code:
onselect="handleSelection()" — Call a named function.onselect="showCount()" — Display selected character count.input.onselect = () => { … } — property assignment.function handleSelection() {
var start = field.selectionStart;
var end = field.selectionEnd;
var selected = field.value.substring(start, end);
count.textContent = selected.length + " characters selected";
}
field.addEventListener("select", handleSelection);| Property / API | When it runs | Notes |
|---|---|---|
select | Text highlighted in field | onselect |
selectionStart | Read anytime | Start index of selection |
selectionEnd | Read anytime | End index of selection |
copy | User copies to clipboard | oncopy |
selectionchange | Any page selection | On document |
| Element | Supported? | Notes |
|---|---|---|
<input type="text"> | Yes | Primary use |
<input type="search">, email, url | Yes | Text-like inputs |
<textarea> | Yes | Multi-line text |
<input type="checkbox"> | No | No selectable text value |
<p>, <div> | No | Use selectionchange on document |
onselect vs oncopy vs selectionchange| Handler | When it fires | Typical use |
|---|---|---|
onselect | Text highlighted in input/textarea | Selection count, format tools |
oncopy | User copies (Ctrl+C) | Analytics, clipboard modify |
selectionchange | Any selection on page | Highlight toolbar on articles |
oninput | Value changes | Live typing — not selection |
Textarea inline handler, dynamic assignment, and selected character count with addEventListener.
Highlight text below — the selection count updates:
0 characters selected
Run a function when the user highlights text:
<textarea onselect="showSelection()">Select some text here</textarea>
<p id="status"></p>
<script>
function showSelection() {
document.getElementById("status").textContent = "Text selected!";
}
</script>When the user selects text inside the textarea, the browser fires select and runs showSelection(). Drag with the mouse or use Shift+arrow keys to highlight text.
Assign the handler on an input at runtime:
<script>
document.getElementById("dynamicInput").onselect = function () {
log.textContent = "Text selected dynamically!";
};
</script>Assigning element.onselect is equivalent to the inline attribute. Use this when you attach the handler after the DOM loads.
Read selectionStart and selectionEnd to show how much text is highlighted:
field.addEventListener("select", function () {
var len = this.selectionEnd - this.selectionStart;
var text = this.value.substring(this.selectionStart, this.selectionEnd);
count.textContent = len + " chars: \"" + text + "\"";
});selectionStart and selectionEnd are zero-based indices into value. Their difference is the number of selected characters.
user-select: none on content users need to copy.select fires for keyboard users.aria-live="polite" when selection count updates.Mouse or keyboard.
onselect runs.
start / end indices.
Count, tools, validation.
The select event and onselect handler are supported in all modern browsers on input and textarea elements.
onselect supportAll major browsers fire select when text is highlighted in form fields.
Bottom line: Reliable for text selection handling on form fields in all modern browsers.
selectionStart / selectionEnd to read highlighted textaddEventListener("select", …) in productionselectionchange for page-wide highlight toolbarsonselect with oncopy (highlight vs clipboard)onselect on plain <div> contentuser-select: none unnecessarilyalert() on every selectiononselect alone for rich-text editors — use dedicated APIsThe onselect attribute runs JavaScript when the user highlights text inside an input or textarea — useful for selection counts, formatting tools, and contextual form help.
Read the selection with selectionStart and selectionEnd, use addEventListener in production, and reach for selectionchange when you need page-wide selection handling.
onselectBookmark these before wiring selection handlers.
input / textarea.
ScopeNot copy.
WhenRead range.
APIClipboard.
CompareWhole page.
Alternativeselect event fires — when the user highlights text inside an input or textarea.<input> elements and <textarea>. Plain paragraphs and divs use selectionchange on the document instead.element.value.substring(element.selectionStart, element.selectionEnd) inside your handler.onselect fires when text is highlighted. oncopy fires when the user copies to the clipboard (Ctrl+C or context menu).addEventListener("select", handler) is preferred in production — easier to attach multiple listeners and keep HTML separate from behavior.Practice the onselect attribute with textarea handlers and character counts in the Try It editor.
5 people found this page helpful