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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.