function selectedText() { if (document.getSelection) { var str = document.getSelection(); if (window.RegExp) { var regstr = unescape("%20%20%20%20%20"); var regexp = new RegExp(regstr, "g"); str = str.replace(regexp, ""); } } else if (document.selection && document.selection.createRange) { var range = document.selection.createRange(); var str = range.text; } else { var str = ""; } return str; }Working example: Try select some text anywhere in this page and press
Get Text Selection
Labels: text