Skip to content


Is there a function to deselect all text using JavaScript?

 

s there a function in javascript to just deselect all selected text? I figure it’s got to be a simple global function like document.body.deselectAll(); or something.

Try this:

function clearSelection()
{
 if (window.getSelection) {window.getSelection().removeAllRanges();}
 else if (document.selection) {document.selection.empty();}
}

This will clear a selection in regular HTML content in any major browser. It won’t clear a selection in a text input or <textarea> in Firefox.

Posted in Javascript, jquery. Tagged with , , .