JavaScript textarea cursor positioning method (compatible with IE and FF) _javascript tips

Source: Internet
Author: User
Today in doing a blog forwarding function, like the Sina Weibo broadcast of that function, there is a textarea, that is, we send a meager box, others have a meager button, when we click on the forwarding button, his meager will enter into the textarea, What looks like a very simple text box assignment is, in fact, the difficulty is that the cursor is positioned at the front.

So he searched the Baidu,google and found the
The method below IE
Copy Code code as follows:

var Tea=document.getelementbyid ("ID of text box");
var txt=textarea.createtextrange ();
Txt.moveend ("character", 0-temptext.text.length);
Txt.select ();

But this method is only available under IE's browser, so a blog was found through web search
Google for a long time, tried a variety of methods, most of them are not support IE is IE only, finally Master told me an old page has this function, found that code tried, ie and FF have been successful!
Share the Code
Copy Code code as follows:

function Locatepoint () {
var Actrl = document.getElementById ("txtcontent");
if (Actrl.setselectionrange) {
settimeout (function () {
Actrl.setselectionrange (0, 0); Position the cursor at the beginning of the textarea, you need to navigate to a different location.
Actrl.focus ();
}, 0);
}else if (actrl.createtextrange) {
var Textarea=document.getelementbyid ("Txtcontent");
var temptext=textarea.createtextrange ();
Temptext.moveend ("character", 0-temptext.text.length);
Temptext.select ();
}
}

Found the method below ff.
Copy Code code as follows:

var Tea=document.getelementbyid ("ID of text box");
Tea.setselectionrange (0, 0); Position the cursor at the beginning of the textarea, you need to navigate to a different location.
Tea.focus ();

So the compatible method we can use an if join to judge, the integration method is as follows, also like that in the post
HTML section
Copy Code code as follows:

<input id= "Tea" type= "text" size= "value=" ">
<button onclick= "xx ()" > Forwarding </button>

JS part
Copy Code code as follows:

<script language= "JavaScript" >
var tea = document.getElementById ("Tea");
Function Locatepoint () {
if (tea.setselectionrange) {
settimeout (function () {
Tea.setselectionrange (0 , 0); Position the cursor at the beginning of the textarea, and you need to navigate to a different location to modify the
Tea.focus ().
}, 0);
}else if (tea.createtextrange) {
var txt=tea.createtextrange ();
Txt.moveend ("character", 0-txt.text.length);
Txt.select ();
}
}
function xx () {
Tea.value = ' BBB ';
Locatepoint ();
}
</script>
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.