views:

245

answers:

3

Hi All,
I would like to get selected text from HTML Input Text Box including character positions.
Please help me.
ant.

A: 

Use Onselect event in javascript. By this you can get the selected things.

pavun_cool
+2  A: 

If you're using jQuery, take a look at the jQuery Caret plugin: jCaret

// Get start pos in intput box with id="box1"
$("#box1").caret().start

// Get end pos
$("#box1").caret().end

// Get selected text
$("#box1").caret().text
Ulf Lindback
A: 

........

<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

Sarfraz