Favorites

HTML onselectionchange Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
onselectionchange
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 22nd November 2026

Description

The onselectionchange event attribute runs a script when the text selection changes. Set on the document (document.onselectionchange), it fires whenever the selection anywhere in the page changes, including when the caret moves; read the selection with document.getSelection().

It can also be used on <input> and <textarea> elements, where it fires when the selection inside the field changes; use the element's selectionStart and selectionEnd properties to read it. The event is not cancelable and does not bubble when fired on the document.

Syntax

<element onselectionchange="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html>
<html>
<head>
<title>onselectionchange event</title>
</head>
<body>
<p>Select some of this text with the mouse or keyboard.</p>
<textarea onselectionchange="document.getElementById('info').textContent = 'Field selection: ' + this.selectionStart + ' to ' + this.selectionEnd">Select text in this field too.</textarea>
<p id="info"></p>
<script>
document.onselectionchange = function () {
var text = document.getSelection().toString();
if (text) document.getElementById("info").textContent = "Selected: " + text;
};
</script>
</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML onselectionchange event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee