views:

58

answers:

2

Hello, I am kind of stuck on this one. I have webapp where I use 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 generally disables mouse text selects on all the browsers. I call it on #desktop element, which is basically main wrapper:

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

I use this, because it disables mouse text selects on all the elements inside #desktop. However, there are some of elements in #desktop, which I want to have normal text select behaviour. Is there some easy way how to implement code, that would make exception to the global rule ?

+4  A: 

JQuery supports the not() selector (here). You could use the distinguishing feature of those elements you wish to have normal behaviour, as the argument to the not() selector

Paul Butcher
I've tried this, but it doesn't seem to work for child elements. Or am I doing something wrong ? $('#desktop').not('table,thead,th,tr,td').disableTextSelect();
Frodik
The problem is that the children of `desktop` inherit the CSS properties. Try to set the CSS values again, using `$('table').css('webkitUserSelect', 'text')` et cetera
Harmen
ok, so basically i would use solution as lonesomeday suggested bellow and implement it as a feature of the plugin to set specific elements with .css() as you said.
Frodik
+1  A: 

The easiest way to do this would be do do some filtering before you start your main 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;});
        }
    });
});
lonesomeday