Skip to content

Instantly share code, notes, and snippets.

@masiuchi
Last active September 29, 2015 02:32
Show Gist options
  • Save masiuchi/58882635af858cf6e4ba to your computer and use it in GitHub Desktop.
Save masiuchi/58882635af858cf6e4ba to your computer and use it in GitHub Desktop.
Select text when clicking.
// http://stackoverflow.com/questions/4165010/use-jquery-to-auto-select-text-inside-a-span-tag-when-clicked
(function($) {
$.fn.clickSelect = function() {
this.click(function() {
var range = document.createRange();
range.setStartBefore(this.firstChild);
range.setEndAfter(this.lastChild);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
});
return (this);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment