JS implementation text box focus in the last position

Source: Internet
Author: User

This article is mainly on the JS implementation of the text box focus in the final position of the sample code is introduced, the need for friends can come to the reference, I hope to help you

In a general program. When the programmer makes the content correctness test of the input box, it usually likes to determine the focus frame by judging the legality of the content. If: The code is as follows: if (obj.value== "") {Obj.focus (); return false; This will move the focus to the input box when the entry box is empty. This feature is very convenient to use.   But there is a small problem ... That's Obj.focus () when you move the focus to the input box, you move the text cursor (that is, a flashing vertical line) to the position of the first character of the input box ... As far as the above judgment is concerned. If there is no content in the text box ...   Obj.focus just satisfies the point where we can just type in the text box without clicking the text box to keep the text focused ... However, if there is already content in the text box ... But this content is illegal. Obj.focus () also moves the cursor to the position of the first character of the text box ... This will let the attention of the user experience of the designer depressed ... What we need is that the text box gets the focus, then the text cursor moves to the end of the text box, allowing the user to enter the content without clicking the text box.   The input will be appended to the original content. The following code can complete this small detail: The code is as follows: <script language= "JavaScript" > function Getselectpos (obj) {var esrc = Document.getelementby Id (obj); if (esrc==null) {esrc=event.srcelement;} var rtextrange =esrc.createtextrange (); Rtextrange.movestart (' character ', esrc.value.length); Rtextrange.collapse (TRUE); Rtextrange.select (); </script> This code is a great help to designers in terms of the details of the user experience ...

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.