I have an old asp.net app originally at .net 2.0 which framework is upgraded to 4.0 (this is not related to my problem, just informing in details) and I'm using Internet Explorer 11. Here I'm facing a problem, I have a text box as shown bellow:
<asp:TextBox ID="txtCost" runat="server" onKeyPress="return GetSelected();" MaxLength="12" TabIndex="6"></asp:TextBox>
In where I'm trying to check on onKeyPress
if the user has selected/highlighted any content and trying to get the selected content. To do this I tried bellow code
JS:
function GetSelected() {
var pass = true;
var content = document.getElementById('<%= txtCost.ClientID %>').value;
if (content.length == 2 && key !== 46) {
if (document.getSelection != undefined) { //true, if there is any selected content
//do something with selected content
pass = false;
}
else //if no content selected then return false
{
pass = false;
}
}
return pass;
}
but here if (document.getSelection != undefined)
is always true even though there is no text selected in the text box. I need an if condition
be true only if there is any selected text in that text box. Well, as I know document.getSelection
returns a selectionRange
object but I'm not very clear how document.getSelection
works. Please help me to fix this.
Bellow are few referrals I followed