Javascript controls insert text at the cursor position for insert of expressions

Source: Internet
Author: User

Directly add the code and use js to control the insert at the cursor position.

Used to insert an emoticon.
Copy codeThe Code is as follows:
<Span style = "font-size: 18px;"> <Head>
<Script type = 'text/javascript '>
Function test (str ){
Var tc = document. getElementById ("mytextarea ");
Var tclen = tc. value. length;
Tc. focus ();
If (typeof document. selection! = "Undefined ")
{
Document. selection. createRange (). text = str;
}
Else
{
Tc. value = tc. value. substr (0, tc. selectionStart) + str + tc. value. substring (tc. selectionStart, tclen );
}
}
</Script>
</Head>
<Body>
<Textarea rows = 5 name = s1 cols = 27 id = "mytextarea"> Objective To insert text at the cursor position in textarea by clicking the button on the page </textarea>
<Input type = button onclick = "test ('this is the text to be added ')"/>
</Body>
</Html>
</Span>

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.