Position the cursor at the far right of the input box to implement the Code-basics

Source: Internet
Author: User

This scenario is often required in the front-end development process. Use the JS implementation to position the cursor at the far right of the input box.

Scene One: Edit the descriptive text of the picture


Scenario Two : Script.aculo.us's Ajax.inplaceeditor class. Double-click to edit, and leave after editing to automatically update the area.

The above scenario requires a JS implementation to position the cursor at the far right of the input box, but not through the mouse point into the input box.

We know that the most basic approach is to htmlelement the focus approach. As follows

Copy Code code as follows:

<p>
<input type= "text" value= "Hello"/>
</p>
<script>
var input = document.getelementsbytagname (' input ') [0];
Input.focus ();
</script>
 

Opening the page will reveal that the cursor is at the left of the input box. The effect is as follows

And now it's going to take three steps to position the cursor at the far right of the input box.

1, call the focus method

2,value assignment is null

3, the value of the previous input is then assigned to yourself

Copy Code code as follows:

<p>
<input type= "text" value= "Hello"/>
</p>
<script>
var input = document.getelementsbytagname (' input ') [0];
var val = input.value;
Input.focus ();
Input.value = ';
Input.value = val;
</script>

After the effect is shown, the cursor is at the far right of the drill box

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.