replace string at clicked position in textarea.

Posted by Abhi_patil under JavaScript on 2/22/2009 | Views : 4049 | Status : [Member] | Replies : 3
i hava textarea with text="How are u JOHN"

now i want, when i rightclick on JOHN that should replace with "JONNY".




Responses

Posted by: Neeks on: 2/22/2009 [Member] Bronze

Up
0
Down
Code for the ASPX page. You have to call the JavaScript for the TextArea for the event oncontextmenu
You have to select the text and then you can change the text by right clicking on it
<form id="form1" runat="server">

<div>
<asp:TextBox runat="server" ID="txtFormat" Text="Hi this is nikhil" TextMode="MultiLine"
oncontextmenu="changeText()"></asp:TextBox>
</div>
</form>

Now you have to write the JavaScript related to the event of the oncontextmenu

JavaScript
<script language="javascript" type="text/javascript">

function getCaret(node)
{
if(node.selectionStart)
{
return node.selectionStart;
}
else if(!document.selection)
{
return 0
}
var c = "\001"
var sel = document.selection.createRange();
var dul = sel.duplicate();
var len = 0;
dul.moveToElementText(node);
sel.text = c;
len = (dul.text.indexOf(c));
sel.moveStart('character',-1);
sel.text = "Tesst"; //New text which you want to replace on the Original text
return len;
}
function changeText()
{
getCaret(document.getElementById('txtFormat'));
}
</script>


Now you can change the Text as you want.
To change the text you have to select it first and then you have to right-click on it. The text will be changed



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

Posted by: Abhi_patil on: 2/23/2009 [Member] Starter

Up
0
Down
thanks nikhil.

but i want just as like when u type any wrong word in ms-office word then it shows with red underline and when u right click on it.that shows all suggestions.
and when u click on any suggestion. that selected word will replace that worng word.

i am very close to my answer as you suggested,but here dont want to select that word,just it should repalce the word on which i will right click.





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

Posted by: Neeks on: 2/24/2009 [Member] Bronze

Up
0
Down
Hi Abhi,
Thanks for the response.
You can replace the JavaScript by following:

<script language="javascript" type="text/javascript">
function getCaret(node)
{
if(node.selectionStart)
{
return node.selectionStart;
}
else if(!document.selection)
{
return 0
}
var c = "\001"
var sel = document.selection.createRange();
var dul = sel.duplicate();
var len = 0;
dul.moveToElementText(node);
sel.text = c;
len = (dul.text.indexOf(c));
var strTest = node.value.substring(0,len);
var strTest1 = node.value.substring(len, node.value.length);
strTest1 = (strTest1.substring(strTest1.indexOf(" "), node.value.length));
strTest = (strTest.substring(0,strTest.lastIndexOf(" ") + 1));
var temp = 0;
var changeVar = "Test";
if(strTest1.indexOf(" ") == -1)
{
strTest1 = "";
}
node.value = strTest + changeVar + strTest1;
node.value = node.value.replace("\001","");
return len;
}
function changeText()
{
getCaret(document.getElementById('txtFormat'));
}
</script>


It there is any further issue please let me know,
Thanks.

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

Login to post response