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

22 Jun, 2021
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:  
javascript
<!DOCTYPE html><html lang="en">    <head>        <meta charset="UTF-8" />        <meta name="viewport"              content="width=device-width,                       initial-scale=1.0" />        <script src="https://code.jquery.com/jquery-3.5.1.min.js"                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
 
sweetyty
JavaScript-Misc
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?