Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created January 25, 2013 23:51
Show Gist options
  • Select an option

  • Save stefanocudini/4638963 to your computer and use it in GitHub Desktop.

Select an option

Save stefanocudini/4638963 to your computer and use it in GitHub Desktop.
plugin jquery for select html element
(function($){
$.fn.selText = function() {
var obj = this[0];
if ($.browser.msie) {
var range = obj.offsetParent.createTextRange();
range.moveToElementText(obj);
range.select();
} else if ($.browser.mozilla || $.browser.opera) {
var selection = obj.ownerDocument.defaultView.getSelection();
var range = obj.ownerDocument.createRange();
range.selectNodeContents(obj);
selection.removeAllRanges();
selection.addRange(range);
} else if ($.browser.safari) {
var selection = obj.ownerDocument.defaultView.getSelection();
selection.setBaseAndExtent(obj, 0, obj, 1);
}
return this;
}
}) (jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment