eads@16: /* eads@16: * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16 eads@16: * (c) 2006 Alex Brem - http://blog.0xab.cd eads@16: */ eads@16: eads@16: (function() { eads@16: eads@16: var fieldSelection = { eads@16: eads@16: getSelection: function() { eads@16: eads@16: var e = this.jquery ? this[0] : this; eads@16: eads@16: return ( eads@16: eads@16: /* mozilla / dom 3.0 */ eads@16: ('selectionStart' in e && function() { eads@16: var l = e.selectionEnd - e.selectionStart; eads@16: return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) }; eads@16: }) || eads@16: eads@16: /* exploder */ eads@16: (document.selection && function() { eads@16: eads@16: e.focus(); eads@16: eads@16: var r = document.selection.createRange(); eads@16: if (r == null) { eads@16: return { start: 0, end: e.value.length, length: 0 } eads@16: } eads@16: eads@16: var re = e.createTextRange(); eads@16: var rc = re.duplicate(); eads@16: re.moveToBookmark(r.getBookmark()); eads@16: rc.setEndPoint('EndToStart', re); eads@16: eads@16: return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text }; eads@16: }) || eads@16: eads@16: /* browser not supported */ eads@16: function() { eads@16: return { start: 0, end: e.value.length, length: 0 }; eads@16: } eads@16: eads@16: )(); eads@16: eads@16: }, eads@16: eads@16: replaceSelection: function() { eads@16: eads@16: var e = this.jquery ? this[0] : this; eads@16: var text = arguments[0] || ''; eads@16: eads@16: return ( eads@16: eads@16: /* mozilla / dom 3.0 */ eads@16: ('selectionStart' in e && function() { eads@16: e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length); eads@16: return this; eads@16: }) || eads@16: eads@16: /* exploder */ eads@16: (document.selection && function() { eads@16: e.focus(); eads@16: document.selection.createRange().text = text; eads@16: return this; eads@16: }) || eads@16: eads@16: /* browser not supported */ eads@16: function() { eads@16: e.value += text; eads@16: return this; eads@16: } eads@16: eads@16: )(); eads@16: eads@16: } eads@16: eads@16: }; eads@16: eads@16: jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; }); eads@16: eads@16: })();