JavaScript Get & Set Cursor Position in Textarea

The Caret is a lightweight jQuery plugin that makes it easy to get & set cursor position with JavaScript in textarea. You can easily get current position of cursor and store it in JavaScript variable. Similarly, you can also set desired cursor position with the help of this plugin.

Moreover, the plugin also support editable div to get and set cursor position.

Plugin Overview

Plugin: Caret
Author: Gideon Sireling
Licence: BSD-3-Clause
Published: January 19, 2024
Repository: Fork on GitHub
Dependencies: jQuery 1.3.1 or Latest version
File Type: zip archive (HTML, CSS & JavaScript )
Package Size: 4.19 KB

NPM Package Installation:

npm i @accursoft/jquery-caret

How to Get & Set Cursor Position in Textarea

1. To getting started using Caret plugin to get textarea cursor position in JavaScript, load the jQuery and plugin JavaScript file into your HTML document.

<!-- jQuery -->
<script src=""></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.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link