Created
December 5, 2014 02:04
-
-
Save joshuarule/ed103301586b277603e9 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/tisitaxumu
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)) { | |
console.log("text selected need to be deselected"); | |
deselectText(); | |
} else { | |
console.log("text non selected need to be selected"); | |
selectText(); | |
} | |
console.log(document.activeElement.id); | |
} | |
</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)) { | |
console.log("text selected need to be deselected"); | |
deselectText(); | |
} else { | |
console.log("text non selected need to be selected"); | |
selectText(); | |
} | |
console.log(document.activeElement.id); | |
} | |
</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)) { | |
console.log("text selected need to be deselected"); | |
deselectText(); | |
} else { | |
console.log("text non selected need to be selected"); | |
selectText(); | |
} | |
console.log(document.activeElement.id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment