How to apply jQuery function to all elements except some elements?

I'm kinda stuck on this. I have a webapp where I use a simple jQuery plugin:

$.extend($.fn.disableTextSelect = function() { return this.each(function(){ if($.browser.mozilla){//Firefox $(this).css('MozUserSelect','none'); }else if($.browser.msie){//IE $(this).bind('selectstart',function(){return false;}); }else if ($.browser.webkit) { // chrome, webkit $(this).css('webkitUserSelect','none'); this.onselectstart = function () { return false; }; }else{//Opera, etc. $(this).mousedown(function(){return false;}); } }); }); 

This plugin usually disables mouse text selection in all browsers. I call it the #desktop element, which is mainly the main shell:

 $('#desktop').disableTextSelect(); 

I use this because it disables mouse text selection in all elements in #desktop. However, there are some elements in #desktop that I want to have regular text selection mode. Is there an easy way to implement code that would make an exception to a global rule?

+4
source share
2 answers

The easiest way to do this is to do some filtering before you start your basic logic.

 $.extend($.fn.disableTextSelect = function() { return this.each(function(){ if ($(this).parents('#excludedItems').length) { //or any other logic here return true; //move to the next item } if($.browser.mozilla){//Firefox $(this).css('MozUserSelect','none'); }else if($.browser.msie){//IE $(this).bind('selectstart',function(){return false;}); }else if ($.browser.webkit) { // chrome, webkit $(this).css('webkitUserSelect','none'); this.onselectstart = function () { return false; }; }else{//Opera, etc. $(this).mousedown(function(){return false;}); } }); }); 
+1
source

JQuery supports the not() selector ( here ). You can use the distinguishing feature of those elements that you want to have normal behavior, since the selector argument is not()

+4
source

All Articles