Open In App

How to insert text into the textarea at the current cursor position?

Last Updated : 18 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To insert text into the textarea/inputbox at the current cursor position we need to get the current position of the cursor. So here the approach to get the current position of the cursor.

  • First, get the current position of cursor with the help of property named as selectionStart on textarea/inputbox.
  • To insert the text at the given position we will use slice function to break the string into two parts and then we will append both parts to the text(text_to_insert) in front and end of the text.

Syntax: 

// will give the current position of the cursor
document.getElementById("id").selectionStart; 

// will get the value of the text area
let x= $('#text1').val();

// will get the value of the input box
let text_to_insert=$('#insert').val();

// setting the updated value in the text area
$('#text1').val(x.slice(0,curPos)+text_to_insert+x.slice(curPos));

Example: In this example, we will insert text into the textarea at the current cursor position

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport"
              content="width=device-width, 
                       initial-scale=1.0" />
        <script src=
                integrity=
"sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" 
                crossorigin="anonymous">
      </script>
        <title>Document</title>
    </head>
    <body>
        <textarea rows="4" 
                  id="text1" 
                  cols="50">
      </textarea>
        <input type="text" 
               id="insert" />
        <button onclick="setTextToCurrentPos()">
          Insert at current position
      </button>
        <script>
            function setTextToCurrentPos() {
                var curPos = 
                    document.getElementById("text1").selectionStart;
                console.log(curPos);
                let x = $("#text1").val();
                let text_to_insert = $("#insert").val();
                $("#text1").val(
x.slice(0, curPos) + text_to_insert + x.slice(curPos));
            }
        </script>
    </body>
</html>


Output: 

Insert text into the textarea at the current cursor position

Insert text into the textarea at the current cursor position

Explanation: Here, A written method is named as setTextToCurrentPos. On the click of Insert at the current position, this method will be called.

//document.getElementById('text1').selectionStart;

this line gives us the current position/start position of the cursor. Example: I am a developer

If our cursor is placed just before the developer. then it will return 7. So as shown in the code using the slice method we can insert the text at the current cursor position.

let x= $('#text1').val(); // will get the value of the text area
let text_to_insert=$('#insert').val(); 
// will get the value of the input box

$('#text1').val(x.slice(0, curPos)+text_to_insert+x.slice(curPos));
// setting the updated value in the text area


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads