Clear Text Selection with JavaScript

 

Using Google chrome



 if (window.getSelection().empty) { 
    window.getSelection().empty();
  }

 

using Firefox

if (window.getSelection().removeAllRanges) {  
    window.getSelection().removeAllRanges();
  }

 

Using Internet Explorer 

if (document.selection) { 
  document.selection.empty();
}

 

Compatibles with all browsers 

if (window.getSelection) {

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

 document.selection.empty();
}

Tags:

Share:

Related posts