This time I will share jQuery Plugin and tutorial about Set And Get Caret Position Using jQuery – Caret.js, hope it will help you in programming stack.
Caret.js is a tiny jQuery plugin created to set and get caret (text cursor) position in textarea
, input
, or contenteditable
element.
See Also:
How to use it:
1. Load the Caret.js script right before the </body>
tag but after jQuery library.
1 |
< script src = "/path/to/cdn/jquery.min.js" ></ script > |
2 |
< script src = "/path/to/jquery.caret.js" ></ script > |
2. Get the caret position.
02 |
< textarea id = "example" > |
03 |
... Any Content Here ... |
07 |
< input id = "example" /> |
10 |
< div id = "example" contenteditable = "true" > |
11 |
... Any Content Here ... |
3. Set the caret position.
1 |
$( '#example' ).caret(1); |
2 |
$( '#example' ).caret(-1); |