To insert a string at the cursor implementation code compatible IE,FIREFOX_JAVASCRIPT tips

Source: Internet
Author: User
Copy Code code as follows:

Inserts a string at the cursor
MyField text Box Object
The value to insert
function Insertatcursor (MyField, myvalue)
{
IE Support
if (document.selection)
{
Myfield.focus ();
sel = Document.selection.createRange ();
Sel.text = myvalue;
Sel.select ();
}
Mozilla/netscape Support
else if (Myfield.selectionstart | | myfield.selectionstart = = ' 0 ')
{
var startpos = Myfield.selectionstart;
var endpos = myfield.selectionend;
Save ScrollTop before insert
var restoretop = myfield.scrolltop;
Myfield.value = myField.value.substring (0, startpos) + myvalue + myField.value.substring (endpos,myfield.value.length) ;
if (Restoretop > 0)
{
Restore previous ScrollTop
Myfield.scrolltop = Restoretop;
}
Myfield.focus ();
Myfield.selectionstart = Startpos + myvalue.length;
Myfield.selectionend = Startpos + myvalue.length;
} else {
Myfield.value + = myvalue;
Myfield.focus ();
}
}

Here is the cloud-dwelling community Demo code:
<script>//Inserts a string at the cursor//MyField text box object//value function to be inserted insertatcursor (MyField, myvalue) {//ie Support if (document.selection) {myfield.focus (); sel = Document.selection.createRange (); Sel.text = myvalue; Sel.select (); }//mozilla/netscape Support Else if (Myfield.selectionstart | | | myfield.selectionstart = = ' 0 ') {var startpos = Myfiel D.selectionstart; var endpos = myfield.selectionend; Save scrolltop before insert var restoretop = myfield.scrolltop; Myfield.value = myField.value.substring (0, startpos) + myvalue + myField.value.substring (endpos,myfield.value.length) ; if (Restoretop > 0) {//restore previous scrolltop myfield.scrolltop = Restoretop; } myfield.focus (); Myfield.selectionstart = Startpos + myvalue.length; Myfield.selectionend = Startpos + myvalue.length; else {myfield.value + = myvalue; Myfield.focus (); } </script> <form> <textarea id=demo > position the mouse to any location here, and then click the button below to test the effect </textarea> <input Type=button onclick=insertatcursor (document.getElementById (' demo '), "cloud-dwelling community") value= cloud-dwelling community id=button1> <input type =button onclick=insertatcursor (document.getElementById (' demo '), "jb51.net") value=jb51.net id=button2> >
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.