Insert a string at the cursor position in textarea

Source: Internet
Author: User

I haven't posted any code for a long time. I am in a good mood today. I moved out to bask in the sun.

VaR getcursorendposition = function (textarea) {textarea. focus (); // first of all, you must select if (textarea. setselectionrange) {// W3C return textarea. selectionend;} else if (document. selection) {// ie var I = 0, OS = document. selection. createRange (), // The current selected area, records the information of the current cursor start and end positions, and can be compared with or to get the current cursor position. OR = Document. Body. createTextRange (); // unavailable or = textarea. createTextRange (); otherwise, compareendpoints cannot be performed with the OS. Or. movetoelementtext (textarea); OS. getbookmark (); for (I = 0; or. compareendpoints ("starttostart", OS) <0 & OS. movestart ("character",-1 )! = 0; I ++) {If (textarea. value. charat (I) = '/N') {// line feed + 1. I ++ ;}} return I ;}}var insertstring = function () {var $ comment =$ ("# comment "); vaR endposition = getcursorendposition ($ comment [0]); var key = This. classname, textvalue = $ comment [0]. value; textvalue = textvalue. substring (0, endposition) + key + textvalue. substring (endposition, textvalue. length); $ ("# comment" + name ). val (textvalue); if ($. browser. MSIE) {// when a change occurs, the position should be-1. Endposition-= textvalue. split ('/N '). length-1; var OR = $ comment [0]. createTextRange (); or. collapse (true); or. movestart ('character ', endposition + 6); or. select ();} else {$ comment [0]. setselectionrange (endposition + 6, endposition + 6 );}});}

 

 

This article uses b3log solo for Synchronous publishing from Vanessa. Original address: http://vanessa.b3log.org/textarea-cursor-insert-string

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.