2 jQuery.fn.getSelection = function() {
3 var e = (this.jquery) ? this[0] : this;
7 ('selectionStart' in e && function() {
8 var l = e.selectionEnd - e.selectionStart;
9 return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
13 (document.selection && function() {
16 var r = document.selection.createRange();
18 return { start: 0, end: e.value.length, length: 0 }
21 var re = e.createTextRange();
22 var rc = re.duplicate();
23 re.moveToBookmark(r.getBookmark());
24 rc.setEndPoint('EndToStart', re);
26 return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
29 // browser not supported
30 function() { return null; }
34 jQuery.fn.replaceSelection = function() {
35 var e = this.jquery ? this[0] : this;
36 var text = arguments[0] || '';
37 var scrollTop = $(this).scrollTop();
41 ('selectionStart' in e && function() {
42 var selectionStart = e.selectionStart;
43 console.log(e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length));
44 e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
45 $(e).scrollTop(scrollTop).focus();
46 e.selectionStart = selectionStart + text.length;
47 e.selectionEnd = selectionStart + text.length;
52 (document.selection && function() {
54 document.selection.createRange().text = text;
58 // browser not supported