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
- HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 7.6.1
Attributions
Mozilla Developer Network : [Selection.isCollapsed Article]
Microsoft Developer Network: [isCollapsed Property Article]