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