2 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
3 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
11 getSelection: function() {
13 var e
= this.jquery
? this[0] : this;
17 /* mozilla / dom 3.0 */
18 ('selectionStart' in e
&& function() {
19 var l
= e
.selectionEnd
- e
.selectionStart
;
20 return { start
: e
.selectionStart
, end
: e
.selectionEnd
, length
: l
, text
: e
.value
.substr(e
.selectionStart
, l
) };
24 (document
.selection
&& function() {
28 var r
= document
.selection
.createRange();
30 return { start
: 0, end
: e
.value
.length
, length
: 0 }
33 var re
= e
.createTextRange();
34 var rc
= re
.duplicate();
35 re
.moveToBookmark(r
.getBookmark());
36 rc
.setEndPoint('EndToStart', re
);
38 return { start
: rc
.text
.length
, end
: rc
.text
.length
+ r
.text
.length
, length
: r
.text
.length
, text
: r
.text
};
41 /* browser not supported */
43 return { start
: 0, end
: e
.value
.length
, length
: 0 };
50 replaceSelection: function() {
52 var e
= this.jquery
? this[0] : this;
53 var text
= arguments
[0] || '';
57 /* mozilla / dom 3.0 */
58 ('selectionStart' in e
&& function() {
59 var cursorlength
= e
.selectionStart
+ text
.length
;
60 e
.value
= e
.value
.substr(0, e
.selectionStart
) + text
+ e
.value
.substr(e
.selectionEnd
, e
.value
.length
);
61 e
.selectionStart
= e
.selectionEnd
= cursorlength
;
66 (document
.selection
&& function() {
67 // get the current cursor position
68 // currently IE 8 does not support methods to get cursor start position
69 // replace below code with the equivalent once method is available
70 var startPosition
= e
.value
.length
;
71 var endPosition
= startPosition
+ text
.length
;
74 e
.value
= e
.value
.substr(0, startPosition
) + text
+ e
.value
.substr( endPosition
, e
.value
.length
);
76 // move the focus to correct position, end of inserted token
78 var range
= e
.createTextRange();
79 range
.move( "character", endPosition
);
84 /* browser not supported */
95 cj
.each(fieldSelection
, function(i
) { cj
.fn
[i
] = this; });