Insert a specific character at the cursor in the JS text box

Source: Internet
Author: User

<% @ Page Language = "C #" AutoEventWireup = "true" AspCompat = "true" CodeBehind = "WebForm1.aspx. cs"
Inherits = "SJLERP. WebForm1" %>


<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Script type = "text/javascript">
Function setCaret (textObj ){
If (textObj. createTextRange ){
TextObj. caretPos = document. selection. createRange (). duplicate ();
}
}
Function insertAtCaret (textObj, textFeildValue ){
If (document. all ){
If (textObj. createTextRange & textObj. caretPos ){
Var caretPos = textObj. caretPos;
CaretPos. text = caretPos. text. charAt (caretPos. text. length-1) = ''? TextFeildValue + '': textFeildValue;
} Else {
TextObj. value = textFeildValue;
}
} Else {
If (textObj. setSelectionRange ){
Var rangeStart = textObj. selectionStart;
Var rangeEnd = textObj. selectionEnd;
Var tempStr1 = textObj. value. substring (0, rangeStart );
Var tempStr2 = textObj. value. substring (rangeEnd );
TextObj. value = tempStr1 + textFeildValue + tempStr2;
} Else {
Alert ("This version of Mozilla based browser does not support setSelectionRange ");
}
}
}
</Script>

<Form id = "form1" action = "" onsubmit = "" method = "post" enctype = "text/plain">
<P>
<Textarea name = "tarea" rows = "" cols = "" style = "width: 300px; height: 120px ;"
Onselect = "setCaret (this );"
Onclick = "setCaret (this );"
Onkeyup = "setCaret (this);"> example: Forget Never subexample: asp.net </textarea>
<Br/>
<Input type = "text" name = "textfield" style = "width: 220px;" value = "insert FireFox"/>
<Br/>
<Input type = "button" value = "insert"
Onclick = "insertAtCaret(this.form.tar ea, this. form. textfield. value);"/>
</P>
</Form>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.