JavaScript :: Remove Selected Text Selection

Created For: http://stackoverflow.com/q/10905943/352449

by Salustiano Silva

HTML

click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all... click to select all...

<br>
<a href="#" id="testCase">click to remove selection</a>

JavaScript

$('#testCase').on("click", function() {

    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();
    }

});