Skip to content

Instantly share code, notes, and snippets.

@joshuarule
Created December 5, 2014 01:22
Show Gist options
  • Save joshuarule/274375f048af41ce4e92 to your computer and use it in GitHub Desktop.
Save joshuarule/274375f048af41ce4e92 to your computer and use it in GitHub Desktop.
Select contentEditable text: http://jsbin.com/wivajetove
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="edit" contenteditable="true">test</div>
<button onclick="toogleSelectionText()">select/deselect</button>
<script id="jsbin-javascript">
var element = document.getElementById('edit');
function selectText () {
var rangeTextSelection = document.createRange(),
selection = window.getSelection();
rangeTextSelection.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(rangeTextSelection);
}
function deselectText() {
window.getSelection().removeAllRanges();
}
function toogleSelectionText () {
var selObj = window.getSelection();
if (selObj.containsNode(element, true)) {
var contentSelection = selObj.toString();
if (contentSelection.length === element.textContent.length) {
console.log("text selected need to be deselected");
deselectText();
} else {
console.log("text partially selected need to be selected");
selectText();
}
} else {
console.log("text non selected need to be selected");
selectText();
}
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">var element = document.getElementById('edit');
function selectText () {
var rangeTextSelection = document.createRange(),
selection = window.getSelection();
rangeTextSelection.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(rangeTextSelection);
}
function deselectText() {
window.getSelection().removeAllRanges();
}
function toogleSelectionText () {
var selObj = window.getSelection();
if (selObj.containsNode(element, true)) {
var contentSelection = selObj.toString();
if (contentSelection.length === element.textContent.length) {
console.log("text selected need to be deselected");
deselectText();
} else {
console.log("text partially selected need to be selected");
selectText();
}
} else {
console.log("text non selected need to be selected");
selectText();
}
}
</script></body>
</html>
var element = document.getElementById('edit');
function selectText () {
var rangeTextSelection = document.createRange(),
selection = window.getSelection();
rangeTextSelection.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(rangeTextSelection);
}
function deselectText() {
window.getSelection().removeAllRanges();
}
function toogleSelectionText () {
var selObj = window.getSelection();
if (selObj.containsNode(element, true)) {
var contentSelection = selObj.toString();
if (contentSelection.length === element.textContent.length) {
console.log("text selected need to be deselected");
deselectText();
} else {
console.log("text partially selected need to be selected");
selectText();
}
} else {
console.log("text non selected need to be selected");
selectText();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment