Created
December 5, 2014 01:22
-
-
Save joshuarule/274375f048af41ce4e92 to your computer and use it in GitHub Desktop.
Select contentEditable text: http://jsbin.com/wivajetove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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