简体   繁体   中英

Mozilla firefox problem in javascript

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!

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)
<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()" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM