From The Blog

Get Selected Text

28 October 2011, in Javascript
Example: get selected and put it in a form textarea
Reference: http://www.codetoad.com/javascript_get_selected_text.asp
<script language=javascript>
function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
    document.aform.selectedtext.value =  txt; //or return the text (as you like)
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()">
<form name=aform >
     <textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>


Post a Comment