Moreover, the plugin also support editable div to get and set cursor position.
NPM Package Installation:
npm i @accursoft/jquery-caret
How to Get & Set Cursor Position in Textarea
1. To getting started using
<!-- jQuery --> <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <!-- Caret JS --> <script src="js/jquery.caret.js"></script>
2. After that, create HTML
textarea element with a unique id.
<textarea id="text"> Some sample text in textarea... </textarea>
3. To get the cursor position, call the plugin with your textarea selector.
var cursor_position = $('#text').caret(); console.log("Cursor position is: "+cursor_position);
4. To set the cursor position use the following syntax.
Pass the value of your desired position in number.
5. The following syntax set the cursor position to the end.
Related Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.