how to get selected text from textbox

Posted by Vanchi050585 under Regular Expressions on 6/2/2011 | Points: 10 | Views : 7142 | Status : [Member] | Replies : 3
hi friends,

i want to get selected text only from textbox. please send the sample code


Thanks in Advance

Thanks
tvn



Responses

Posted by: Lakn2 on: 6/2/2011 [Member] Starter | Points: 25

Up
0
Down
try this

<form id="form1" runat="server">

<div>

<asp:TextBox runat="server" ID="txt1" TextMode="MultiLine" />

<asp:HiddenField runat="server" ID="hidSelectedText" />

<asp:Button runat="server" ID="showselected" Text="Show selected" OnClientClick="doselection();" OnClick="showselected_Click" />

<script type="text/javascript">

function doselection()
{

if (window.getSelection)
{

txt = window.getSelection();

}

else if (document.getSelection)
{

txt = document.getSelection();

}

else if (document.selection)
{

txt = document.selection.createRange().text;

}

else

{

alert('selection not supported');
}

document.getElementById('<%=hidSelectedText.ClientID %>').value = txt;
}

</script>

</div>

</form>



-----------------------------------------------------------------------

protected void showselected_Click(object sender, EventArgs e)
{

Response.Write(hidSelectedText.Value);

}




Thanks&Regards
LakshmiNarayana Nalluri.

Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/2/2011 [Member] Starter | Points: 25

Up
0
Down
Javascript
<script language="javascript" type="text/javascript">

function getInputSelection(el) {
el = document.getElementById(el);
var start = 0, end = 0, normalizedValue, range, textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart; end = el.selectionEnd;
}
else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length; normalizedValue = el.value.replace(/\r\n/g, "\n");
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else { start = -textInputRange.moveStart("character", -len); start += normalizedValue.slice(0, start).split("\n").length - 1; if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) { end = len; } else { end = -textInputRange.moveEnd("character", -len); end += normalizedValue.slice(0, end).split("\n").length - 1; } }
}
}
if (start == end)
return "";
else
return el.value.slice(start, end);
}
</script>

in aspx page
<asp:TextBox ID="TextBox1"  runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<input id="Button1" onclick="document.getElementById('<%= TextBox2.ClientID %>').value=getInputSelection('<%= TextBox1.ClientID %>')" type="button" value="button" />

Reference
/* Answer from Tim Down http://stackoverflow.com/questions/3964710/replacing-selected-text-in-the-textarea*/

Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A4u6178 on: 6/2/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

refer the code below..


<html>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function showselected() {
var component = document.getElementById("Label1");
var selectedText;
if (document.selection != undefined) {
component.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
alert("You selected :" + selectedText);
}
}
</script>
</head>
<body>
<form>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="showselected()" />
</form>
<body>
</html>

Thanks & Regards;
aaa

Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response