Mark the light on the far right of the input box to implement code _ basic knowledge

Source: Internet
Author: User
How to calibrate the implementation method on the far right of the input box is required by many people. This article will introduce in detail. For more information, see

This scenario is often required during front-end development. Use JS to calibrate the light on the rightmost side of the input box.

Scenario 1: edit the description of an image


Scenario 2: Script. aculo. us Ajax. InPlaceEditor class. You can double-click it to edit it. You can automatically update the region after editing it.

All of the above scenarios require JS to achieve optical calibration at the far right of the input box, but not by clicking the mouse into the input box.

We know that the most basic method for implementation is the focus Method of HTMLElement. As follows:

The Code is as follows:





Script
Var input = document. getElementsByTagName ('input') [0];
Input. focus ();
Script

 

Open this page and you will find that the cursor is at the far left of the input box. The effect is as follows:

Now we need to implement three steps to set the optical calibration to the far right of the input box.

1. Call the focus method.

2. The value assignment is null.

3. Assign the previous input value to yourself.

The Code is as follows:





Script
Var input = document. getElementsByTagName ('input') [0];
Var val = input. value;
Input. focus ();
Input. value = '';
Input. value = val;
Script


After running, the cursor is on the rightmost side of the in-depth box.

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.