isCollapsed property
Retrieves whether a selection is collapsed or empty.
![]() ![]() |
Syntax
| JavaScript | |
|---|
Property values
Type: Boolean
One of the following values:
Standards information
- HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 7.6.1
Remarks
A collapsed selection has its start and end points set to the same value, which renders it 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>
<meta http-equiv="X-UA-Compatible" content="IE=9" /> <!-- Only works in Internet Explorer 9 -->
<!-- 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>
See also
Show:

