How to disable text selection using jQuery?

Dawid Ohia picture Dawid Ohia · Apr 23, 2010 · Viewed 195.9k times · Source

Does jQuery or jQuery-UI have any functionality to disable text selection for given document elements?

Answer

SLaks picture SLaks · Apr 23, 2010

In jQuery 1.8, this can be done as follows:

(function($){
    $.fn.disableSelection = function() {
        return this
                 .attr('unselectable', 'on')
                 .css('user-select', 'none')
                 .on('selectstart', false);
    };
})(jQuery);