I am trying to remove selection inside a contenteditable field. The code I have is this:
Text
I found this on SO, and should be what you are looking for:
if (window.getSelection) {
if (window.getSelection().empty) { // Chrome
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) { // Firefox
window.getSelection().removeAllRanges();
}
} else if (document.selection) { // IE?
document.selection.empty();
}
Working fiddle
An answer here solved my problem, but it got deleted for some reason. It was this:
Replacing $('h1').blur()
with window.getSelection().removeAllRanges();"
fixed it.
here is the working code
$(document).ready(function() {
$('h1').focus();
document.execCommand('selectAll', false, null);
})
$("#deselect").click(function(){
window.getSelection().removeAllRanges();
});
https://jsfiddle.net/v73e3xdf/2/