Home > Web Front-end > JS Tutorial > body text

How to position the cursor at the end of text in a text input field using JavaScript?

王林
Release: 2023-08-27 14:09:17
forward
1230 people have browsed it

如何使用 JavaScript 将光标位置放置在文本输入字段中的文本末尾?

In HTML, input fields are used to get input from the user. Sometimes, we need to get a string or long text message in the input. In these scenarios, the user may need to go to the end of the input field to add more content or messages. So we should set something up so that the user can click on the button and can go to the end of the input field. In this tutorial, we will learn to use JavaScript to position the cursor at the end of text in an input field.

Use setSelectionRange() method

setSelectionRange() method is used to select text in the input field. Two parameters are required to start and end the selection. We can use the setSelectionRange() method to select the last character, placing the cursor at the end of the text.

grammar

Users can use the setSelectionRange() method according to the following syntax to place the cursor at the end of the text.

input.setSelectionRange(len, len);
Copy after login

In the above syntax, the input is the HTML element that we access in JavaScript. "len" is the length of the input value.

Example

In the example below, we create input fields and buttons. When the user clicks the button, it executes the moveAtend() function.

In JavaScript, we define the moveAtend() function, which accesses the "name" input field. After that, it sets focus on the input field using the focus() method. After that, we use the setSelectionRange() method by passing the length of the input value as two parameters. In the output, the user can click the button and watch it set the cursor position to the end of the text.

<html>
   <body>
      <h3> Using the <i> setSelectionRange() method </i> to move the cursor at the end of the input field in JavaScript </h3>
      <input type = "text" id = "name" value = "Shubham">
      <button onclick = "moveAtend()"> Move cursor at end </button>
      <script>
         let output = document.getElementById("output");
         function moveAtend() {
         let name = document.getElementById("name");
         name.focus();
         name.setSelectionRange(name.value.length, name.value.length);
         }
      </script>
</html>
Copy after login

Use selectionStart and selectionEnd properties

The "selectionStart" and "selectionEnd" CSS properties are used to select input values ​​in input fields. "selectionStart" takes the index value from where we started the selection, and "selectionEnd" takes the index value where we need to end the text selection.

Here we can set the value of the "selectionStart" and "selectionEnd" properties equal to the text length to place the cursor at the end of the text.

grammar

Users can use the "selectionStart" and "selectionEnd" attributes according to the following syntax to place the cursor at the end of the text.

input.selectionStart = len;
input.selectionEnd = len;
Copy after login

In the above syntax, input is an HTML element and 'len' is the length of its value.

Example

Like the first example, we create the input fields in the example below. In the moveAtend() function, we set the value of the "selectionStart" and "selectionEnd" properties equal to the text length of the "name" input field.

<html>
   <body>
      <h3> Using the <i> selectionStart and selectionEnd Properties </i> to move the cursor at the end of the input field in JavaScript </h3>
      <input type = "text" id = "name" value = "Shubham">
      <button onclick = "moveAtend()"> Move cursor at end </button>
      <script>
         let output = document.getElementById("output");
         function moveAtend() {
         let name = document.getElementById("name");
         name.focus();
         name.selectionStart = name.value.length;
         name.selectionEnd = name.value.length;
         }
      </script>
</html>
Copy after login

in conclusion

We learned two ways to position the cursor at the end of a text value in an input field. In the first method, we used the setSelectionRange() method; in the second method, we used the "selectionStart" and "selectionEnd" properties. However, both methods are almost identical, the setSelectionRange() method takes the "selectionStart" and "selection" end values ​​as parameters.

The above is the detailed content of How to position the cursor at the end of text in a text input field using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template