You are here: Reference > JavaScript > client-side > event handling > events > onselectionchange

onselectionchange event | selectionchange event

Browser support:
Occurs when the selection in the document has changed.
To get the current selection within the document, use the selection object in Internet Explorer.

How to register:

In HTML:
This event cannot be registered in HTML.

In JavaScript:
object.onselectionchange = handler;
object.addEventListener ("selectionchange", handler, useCapture);
9
object.attachEvent ("onselectionchange", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles No
Cancelable No
Event object Event

Actions that invoke the onselectionchange event:

  • Mofifying the current selection or the position of the caret.
  • Changing the selection by script (through the selection object or with the select method).

The order of events related to the onselectionchange event:

  1. onselectstart
  2. onselect
  3. onselectionchange

Example HTML code 1:

This example illustrates the use of the onselectionchange event:
<head>
    <script type="text/javascript">
        document.onselectionchange = OnChange;
        function OnChange () {
            var range = document.selection.createRange ();
            if (range.text.length == 0) {
                alert ("The current selection is empty");
            }
            else {
                alert ("The contents of the current selection are\n" + range.text);
            }
        }
    </script>
</head>
<body>
    Select this text on this page!
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content