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!
Solution 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)
Solution 2:
Solution 3:
<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()" />
Post a Comment for "Mozilla Firefox Problem In Javascript"