jQuery Plugin: .selection()

Wrote a new plugin today. This one will get the current selection of any input, and also (optionally) change the input.

/**
* jQuery Selection Plugin
*
* copyright (c) 2007 Woody Gilk
* woody.gilk@gmail.com
*
* Released under BSD license
*
* Get selection: $(elem).selection();
* Set selection: $(elem).selection('foo');
*
* NOTES: MSIE doesn't use the move_to parameter correctly, either because I suck,
* or MSIE sucks, take your pick. Please email me if you know how to fix it!
*/
jQuery.fn.selection = function(){
var element = (this.jquery) ? this[0] : this;
var replace = arguments[0] || false;
var move_to = arguments[1] || false;
// Need to focus the given element to extract the selection
element.focus();
// IE sux!
if (jQuery.browser.msie) {
var s = document.selection.createRange();
// Return or replace
if (replace == false) {
return s.text;
} else {
s.text = replace;
if (move_to > 0) {
var m = document.selection.createRange();
m.moveToBookmark(s.getBookmark());
m.collapse(true);
m.move(’character’, move_to);
m.select();
}
return this;
}
} else {
var s = element.selectionStart;
var e = element.selectionEnd;
// Return or replace
if (replace == false) {
return element.value.substr(s, (e - s));
} else {
element.value = element.value.substr(0, s) + replace + element.value.substr(e, element.value.length);
if (move_to > 0) { element.setSelectionRange(s+move_to, s+move_to); }
return this;
}
}
};

Comments

Leave a Reply