Alex Rivera | Logout

Get selected text in a textbox

Asked 2010-03-04T08:45:46.317
9

How can I get the character positions of the selected text in an HTML <input> textbox element? window.getSelection() doesn't work inside textboxes.

Edit
Report

1 Answer

3

........

<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;
}
</script>

<input type="button" value="Get selection" onmousedown="getSelText()"> 

<form name=aform >
<textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>

Reference: http://www.codetoad.com/javascript_get_selected_text.asp

answered 2010-03-04T08:59:51.617

Your Answer