query or JavaScript implementation inserts text at the TEXTAREA cursor

Source: Internet
Author: User

1.Jquery function Implementation:

$(function() {    /*insert text at textarea--start*/    (function($) {$.fn.extend ({insertcontent:function(myvalue, t) {var$t = $ ( This) [0]; if(document.selection) {//IE                             This. focus (); varSEL =Document.selection.createRange (); Sel.text=myvalue;  This. focus (); Sel.movestart (' Character ',-l); varWee =sel.text.length; if(Arguments.length = = 2) {                                varL =$t. value.length; Sel.moveend ("Character", Wee +t); T<= 0? Sel.movestart ("character", Wee-2 *T-myvalue.length): Sel.movestart ("Character", Wee-t-myvalue.length);                            Sel.select (); }                        } Else if($t. SelectionStart|| $t. SelectionStart = = ' 0 ') {                            varStartpos =$t. SelectionStart; varEndpos =$t. Selectionend; varScrollTop =$t. scrolltop; $t. Value= $t. value.substring (0, Startpos)+myvalue+$t. value.substring (Endpos, $t. value.length);  This. focus (); $t. SelectionStart= Startpos +myvalue.length; $t. Selectionend= Startpos +myvalue.length; $t. scrolltop=scrolltop; if(Arguments.length = = 2) {$t. Setselectionrange (startpos-T, $t. Selectionend+t);  This. focus (); }                        } Else {                             This. Value + =myvalue;  This. focus ();    }})}) (JQuery); /*insert text at textarea--ending*/});

Call Method:

$ (Text field selector). Insertcontent ("inserted content");
$ (Text field selector). Insertcontent ("inserted content", numeric value); Selects the boundary of the inserted text content according to the value, the value: 0 is the full selection of the inserted text, 1 indicates that one character is less selected on each side of the inserted text.

functionInsertText (obj,str) {if(document.selection) {varSEL =Document.selection.createRange (); Sel.text=str; } Else if(typeofObj.selectionstart = = = ' Number ' &&typeofObj.selectionend = = = ' Number ') {        varStartpos =Obj.selectionstart, Endpos=Obj.selectionend, Cursorpos=startpos, Tmpstr=Obj.value; Obj.value= tmpstr.substring (0, startpos) + str +tmpstr.substring (Endpos, tmpstr.length); Cursorpos+=str.length; Obj.selectionstart= Obj.selectionend =Cursorpos; } Else{Obj.value+=str; }}functionmoveEnd (obj) {obj.focus (); varLen =obj.value.length; if(document.selection) {varSEL =Obj.createtextrange (); Sel.movestart (' Character ', Len);        Sel.collapse ();    Sel.select (); } Else if(typeofObj.selectionstart = = ' Number ' &&typeofObj.selectionend = = ' Number ') {Obj.selectionstart= Obj.selectionend =Len; }} 

query or JavaScript implementation inserts text at the TEXTAREA cursor

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.