You are here: Reference > JavaScript > client-side > selection and ranges > methods > empty (selection)

empty method (selection)

Browser support:
Cancels the current selection.
To remove the contents of the current selection from the document, use the clear method.
In Firefox, Opera, Google Chrome and Safari, use the removeAllRanges method to cancel the selection.

Syntax:

object.empty ( );
You can find the related objects in the Supported by objects section below.

Return value:

This method has no return value.

Example HTML code 1:

This example shows how to cancel the selection in different browsers:
<head>
    <script type="text/javascript">
        function CancelSelection () {
            if (window.getSelection) {  // all browsers, except IE before version 9
                var myRange = window.getSelection ();                                        
                myRange.removeAllRanges ();                                        
            }
            else {
                document.selection.empty ();
            }
        }
    </script>
</head>
<body>
    Select some text!<br />
    <button onclick="CancelSelection ()">Cancel the current selection!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content