yoeran / jquery-caret

Simple caret moving mofo jQuery plugin.

Home Page:https://github.com/DrPheltRight/jquery-caret

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jQuery Caret

This is a very simple lightweight plugin to allow you to move the caret (or cursor) position in an <input /> or <textarea> element and/or modify text according to position and selection.

$.fn.caret()

Use this method with no parameters to get the current position of the caret within the first element matched.

var position = $('input').caret();

$.fn.caret( index , [ offset ] )

This methods first parameter is the index of where you want to move the caret to. In order to move to an index, index must be an integer.

Alternatively you can pass a string as an index and it will be used via .indexOf() the element's value to get an index to move to.

The second parameter is to be used to move the caret to an offset of the index. When set to true, it will move the cursor after the string if a string was passed.

$('input').caret(10);

$.fn.caretToEnd()

This method moves the caret to the end of the content within your element, also for your convenience.

$('input').caretToEnd();

$.fn.insertAtCaret()

This method inserts text at the current caret position.

$('textarea').insertAtCaret('text');

$.fn.replaceAtCaret()

This method replaces the selected text and will be maintained if you add two % signs in your string. You could use this to surround a selected text with -tags (or MarkDown codes).

$('textarea').replaceAtCaret('<strong>%%</strong>');

Author(s)

License

MIT

About

Simple caret moving mofo jQuery plugin.

https://github.com/DrPheltRight/jquery-caret


Languages

Language:JavaScript 100.0%