How to use JavaScript to obtain the cursor position in textarea

Source: Internet
Author: User
Write an online editor for UBB, because the pop-up window is not used Code At the end of textarea, I could not insert the cursor before, so I went online and went crazy to find information. I finally found out how to get the position in textarea, however, if textarea has a lot of content, it will flash. The Code is as follows.

Function getpos (OBJ)
{
OBJ. Focus ();
VaR workrange = Document. selection. createRange ();
OBJ. Select ();
VaR allrange = Document. selection. createRange ();
Workrange. setendpoint ("starttostart", allrange );
VaR Len = workrange. Text. length;
Workrange. Collapse (false );
Workrange. Select ();

Return Len;
}

At this time, the problem arises. In obj. Select (), the flash will occur and the scroll bar cannot be reset. As a result, the younger brother rewrite the Code as follows:

Function getcaret (zysrid)
{
VaR TXB = Document. getelementbyid (zysrid); // obtain the object by ID
VaR Pos = 0; // set the initial position
TXB. Focus (); // The input box gets the focus. This sentence should not be too short. Otherwise, there will be errors and blood lessons.
VaR S = TXB. scrolltop; // obtain the position of the scroll bar.
VaR r = Document. selection. createRange (); // create a document Selection object
VaR T = TXB. createTextRange (); // create a Text object in the input box
T. Collapse (true); // move the cursor to the header
T. Select (); // display the cursor. This should not be small. Otherwise, the cursor is not moved to the header. I didn't know it at the time. It took 10 minutes.
VaR J = Document. selection. createRange (); // create a document Selection object for the new cursor position
R. setendpoint ("starttostart", J); // create an object between the selected object and the new object in the previous document. It's hard to explain it. I'm not very expressive. if you are interested, go to the msdn documents.
VaR STR = R. Text; // obtain the object text
VaR Re = new Regexp ("[\ n]", "G"); // filter out line breaks, or your text may be faulty, it will be longer than the actual length of your text. it killed me. I said how the number I got is always longer than my actual length.
STR = Str. Replace (Re, ""); // Filter
Pos = Str. length; // obtain the length, that is, the cursor position.
R. Collapse (false );
R. Select (); // restore the cursor to its previous position
TXB. scrolltop = s; // restore the scroll bar to its previous position.
}

// Set the cursor Function

Function setcaret (ID, POS)
{
VaR textbox = Document. All (ID );
VaR r = textbox. createTextRange ();
R. Collapse (true );
R. movestart ('character ', POS );
R. Select ();
}

In fact, this is not difficult, but when you do not know it, it will lead to death. I tried it for about five hours to achieve this effect. I hope this will help some brothers.
Related Article

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.