2 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
3 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
10 getSelection: function() {
12 var e = this.jquery ? this[0] : this;
16 /* mozilla / dom 3.0 */
17 ('selectionStart' in e && function() {
18 var l = e.selectionEnd - e.selectionStart;
19 return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
23 (document.selection && function() {
27 var r = document.selection.createRange();
29 return { start: 0, end: e.value.length, length: 0 }
32 var re = e.createTextRange();
33 var rc = re.duplicate();
34 re.moveToBookmark(r.getBookmark());
35 rc.setEndPoint('EndToStart', re);
37 return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
40 /* browser not supported */
42 return { start: 0, end: e.value.length, length: 0 };
49 replaceSelection: function() {
51 var e = this.jquery ? this[0] : this;
52 var text = arguments[0] || '';
56 /* mozilla / dom 3.0 */
57 ('selectionStart' in e && function() {
58 e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
63 (document.selection && function() {
65 document.selection.createRange().text = text;
69 /* browser not supported */
81 jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });