Is there a function to deselect all text using JavaScript?

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.

Leave a Comment