This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

isCollapsed

Summary

Retrieves whether a selection is collapsed or empty.

Property of dom/Selectiondom/Selection

Syntax

Note: This property is read-only.

var result = selObj.isCollapsed;

Return Value

Returns an object of type BooleanBoolean

false - The selection is not collapsed.

true - The selection is collapsed or empty.

Examples

This example uses isCollapsed to see whether a selection is empty or not. If the selection is not empty, the selected text is displayed.

<!DOCTYPE html>

<html>
  <head>


    <!-- This example shows the character offset from anchor node of your selection. -->
    <title>isCollapsed Example</title>
    <script type="text/javascript">
      function testCollapsed () {
        if (window.getSelection) {
          var oSel = window.getSelection();
          var oDisplayBox = document.getElementById('displayBox');

          if (oSel.isCollapsed) {
            oDisplayBox.innerHTML = "<p>Nothing is selected!</p>";
          }
          else {
            oDisplayBox.innerHTML = "<p>The text of the selection:\n" + oSel.toString() + "</p>";
          }
        }
      }
    </script>
  </head>

  <body>
    <p>
      Click the button below to get the text content.
      Use the mouse to select some text within this field.
      Then, click the button again to see if the selection is collapsed.
    </p>
    <input type="button" value="Is it collapsed" onclick="testCollapsed()" />
    <div id="displayBox"></div>
  </body>
</html>

Notes

Remarks

A collapsed selection has its start and end points set to the same value, which renders it empty. Even a collapsed selection may have a rangeCount greater than 0. selObj.getRangeAt(0) may return a range that is also collapsed.

Syntax

Standards information

Attributions