Retrieves whether a selection is collapsed or empty.
Property of dom/Selectiondom/Selection
SyntaxNote: 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.
ExamplesThis 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>
<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 informationMozilla Developer Network : [Selection.isCollapsed Article]
Microsoft Developer Network: [isCollapsed Property Article]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4