Press Esc. Click with any tool (including the Arrow) on an empty part of the project to deselect all the selected elements at once. Click a selected element while holding down the Shift key.
body. deselectAll(); or something.
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();
}
Credit to Mr. Y.
Best to test the features you want directly:
var sel = window.getSelection ? window.getSelection() : document.selection;
if (sel) {
if (sel.removeAllRanges) {
sel.removeAllRanges();
} else if (sel.empty) {
sel.empty();
}
}
I did some research of my own. Here's the function I wrote and am using these days:
(function deselect(){
var selection = ('getSelection' in window)
? window.getSelection()
: ('selection' in document)
? document.selection
: null;
if ('removeAllRanges' in selection) selection.removeAllRanges();
else if ('empty' in selection) selection.empty();
})();
Basically, getSelection().removeAllRanges()
is currently supported by all modern browsers (including IE9+). This is clearly the correct method moving forward.
Compatibility issues accounted for:
getSelection().empty()
document.selection.empty()
It's probably a good idea to wrap up this selection functionality for re-use.
function ScSelection(){
var sel=this;
var selection = sel.selection =
'getSelection' in window
? window.getSelection()
: 'selection' in document
? document.selection
: null;
sel.deselect = function(){
if ('removeAllRanges' in selection) selection.removeAllRanges();
else if ('empty' in selection) selection.empty();
return sel; // chainable :)
};
sel.getParentElement = function(){
if ('anchorNode' in selection) return selection.anchorNode.parentElement;
else return selection.createRange().parentElement();
};
}
// use it
var sel = new ScSelection;
var $parentSection = $(sel.getParentElement()).closest('section');
sel.deselect();
I've made this a community wiki so that you people can add functionality to this, or update things as the standards evolve.
2021 answer
removeAllRanges()
is supported by most browsers, except Safari on macOS or iOS.
empty()
is an alias for removeAllRanges()
and supported by all browsers, including very old ones, with the exception of IE. This alias is defined in the specification, so should be safe to rely on.
Conclusion
Just use getSelection().empty()
. There is no need for helper functions, nested ternary ifs, constructors, and whatnot Ninja banzai from the other answers. Perhaps needed ten years ago, but not any more.
If you really need IE support you can test for document.selection
:
(window.getSelection ? window.getSelection() : document.selection).empty()
(not tested on IE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With