0

how i can get (if)any text is selected in textbox and i want to get it in any variable of javascript.... specifically for Mozilla firefox...? Thanks in advance!

jyotin
  • 48
  • 8
  • Note: the above description is not enough so let me give completely the definition.. My Extension of firefox is an Extension that double clicks any word from the webpage and finds its possible meaning from database... so user can even write anything in Textbox and double click the same for finding its meaning.. so please do suggest any way to complete selection from textbox's selected text....? in addition i am already using dblclick event handler so dont suggest that solution.... Thanxx in advance – jyotin Feb 08 '11 at 11:28

3 Answers3

1

This should work:

 alert(document.getElementById('TextBoxID').value);

And asigning that value to some variable:

 var variablename = document.getElementById('TextBoxID').value

Edit: I just saw that you want to read only the selected text. This can be done this way:

 if (TextBox.selectionStart != undefined)
  {
    var startPos = TextBox.selectionStart;
    var endPos = TextBox.selectionEnd;
    var selectedText = TextBox.value.substring(startPos, endPos)
   }
  alert("You selected: " + selectedText);
}

If you only need to know if a user has selected anything, you can do:

var hasSelected = (TextBox.selectionStart != undefined)
Chris
  • 8,649
  • 16
  • 55
  • 73
0

Have a look at How to get selected text from textbox control with javascript?

Community
  • 1
  • 1
Reto Aebersold
  • 15,409
  • 4
  • 49
  • 69
0
<script type="text/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()" /> 
Barrie Reader
  • 10,474
  • 11
  • 67
  • 132