Skip to content
Related Articles

Related Articles

How to insert text into the textarea at the current cursor position?
  • Last Updated : 21 Jul, 2020

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.

    Approach:

  • 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 postion 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:




<!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:

Output:1



After clicking on Insert at current position button

Explaination:

Here, A written method named as setTextToCurrentPos. On the click of Insert at 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 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :