-
Notifications
You must be signed in to change notification settings - Fork 0
/
textarea-helper.js
64 lines (61 loc) · 2.06 KB
/
textarea-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function ($) {
insertAtCaret = function (areaId, text) {
var txtarea = document.getElementById(areaId);
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br =
txtarea.selectionStart || txtarea.selectionStart == "0"
? "ff"
: document.selection
? "ie"
: false;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart("character", -txtarea.value.length);
strPos = range.text.length;
} else if (br == "ff") strPos = txtarea.selectionStart;
var front = txtarea.value.substring(0, strPos);
var back = txtarea.value.substring(strPos, txtarea.value.length);
txtarea.value = front + text + back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
range = document.selection.createRange();
range.moveStart("character", -txtarea.value.length);
range.moveStart("character", strPos);
range.moveEnd("character", 0);
range.select();
} else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
};
$.fn.getCursorPosition = function () {
var el = $(this).get(0);
var pos = 0;
if ("selectionStart" in el) {
pos = el.selectionStart;
} else if ("selection" in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart("character", -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
};
$.fn.setCursorPosition = function (pos) {
if ($(this).get(0).setSelectionRange) {
$(this).get(0).setSelectionRange(pos, pos);
} else if ($(this).get(0).createTextRange) {
var range = $(this).get(0).createTextRange();
range.collapse(true);
range.moveEnd("character", pos);
range.moveStart("character", pos);
range.select();
}
};
})(jQuery);