About how to locate the textarea cursor in JS

Source: Internet
Author: User
Regarding the JS positioning of textarea cursor, I first issued an unfortunate statement: I have been engaged in PHP & nbsp; for a year and a half. during this New Year, our team lead told me that the company boss said, the PHP project will not make any money and will be disbanded soon. in January 15, the PHP project team & nbsp; all went viral. well, there is no good project bonus in the New Year's Eve, how to locate the textarea cursor in JS
First of all, I made an unfortunate statement that I have been using PHP for a year and a half. during the Chinese New Year, our team lead told me that the company boss said that the PHP project will not make money and will soon be disbanded, in January 15, the PHP project team went viral. Okay, well, during the New Year's Eve, we didn't have a good project bonus, and we didn't have a good year's total prize. the technical director also went away. okay, what qualifications do I have to say I am not leaving. When you go home, ask me where you are working, what do you do? How can I answer this question? Said I was fired?

Well, after the above grief is vented, I will solve my last technical problem in the company, and then prepare soy sauce to wait for the egg.

Now I want to create a tag code insert template function in the background,
When I click insert tag, an IFRAME will be opened and a tag will be generated after various options above, and then inserted to the cursor position in the starting textarea,
I can't use JS. I also see a JS on CSDN.
As follows:
Function insertText (charvalue)
{
Var obj = document. getElementById ("templateContent ");
Obj. focus ();
Var leng = obj. value. length;
If (typeof document. selection! = "Undefined") // ie
{
Var r = document. selection. createRange ();
R. text = charvalue;
}
Else // firefox
{
Obj. value = obj. value. substr (0, obj. selectionStart) + charvalue + obj. value. substring (obj. selectionStart, leng );
}
}

My principle is to generate the label above and call this function to insert it to the cursor position of textarea. the above test shows that Firefox is normal, but in IE, click "Insert tag" to open the window. if you do not click the text box, you can insert it to the cursor position of textarea. However, if you click the text box in the window, the cursor appears, the generated tag is inserted at the beginning of textarea.

Okay, I don't know. you may understand my description. if you don't understand it, you can leave a comment to solve the compatible JS in IE. what should I do?
I
------ Solution --------------------
Js questions should have been asked by the js version. this event involving js also involves cross-browser. php is not an event trigger mechanism and is not necessarily understandable.
I have been dropping JavaScript for many years and cannot answer this question.

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.