Here's a version that will clear any selection, including in text inputs and text areas:
Demo: http://jsfiddle.net/SLQpM/23/
function clearSelection() { var sel; if ( (sel = document.selection) && sel.empty ) { sel.empty(); } else { if (window.getSelection) { window.getSelection().removeAllRanges(); } var activeEl = document.activeElement; if (activeEl) { var tagName = activeEl.nodeName.toLowerCase(); if ( tagName == "textarea" || (tagName == "input" && activeEl.type == "text") ) {
Tim Down Feb 09 '13 at 12:44 2013-02-09 12:44
source share