4

I've got this snippet of code to disable all text selection. How would I go about disabling all text except for input? I tried $('* :not(input)').disableTextSelect(); but it disabled selection for everything (input included)

$.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 {//Opera, etc.
                        $(this).mousedown(function () { return false; });
                    }
                });
            });
            $('* :not(input)').disableTextSelect(); 
2
  • Are you sure the * is necessary? Try $(":not(input)") Commented Jun 9, 2011 at 22:21
  • 1
    Note that disabling selection changes the default behaviour of the browser and will confuse or even annoy the user. Commented Jun 9, 2011 at 22:24

3 Answers 3

14
$(document).bind('mousedown selectstart', function(e) {
    return $(e.target).is('input, textarea, select, option, html');
});

Thanks to @user2873592, who mentioned that adding html here would fix the chrome scroll bar can't be dragged issue.

Sign up to request clarification or add additional context in comments.

1 Comment

just pasted it inside document.ready and it worked like a charm !! thanks a lot @deerchao
5

This works in IE and FF:

    jQuery(document).ready(function () {
        //Disable default text selection behavior
        toggleEnableSelectStart(false);

        //for inputs it must be possible to select text
        jQuery("input[type=text]").focusin(function () { toggleEnableSelectStart(true); });
        jQuery("input[type=text]").mouseover(function () { toggleEnableSelectStart(true); });
        jQuery("input[type=text]").focusout(function () { toggleEnableSelectStart(false); });
        jQuery("input[type=text]").mouseout(function () { toggleEnableSelectStart(false); });

    });

    function toggleEnableSelectStart(enable) {
        document.onmousedown = function (e) { return enable; };
        document.onselectstart = function (e) { return enable; }; ;
    }

1 Comment

Works for me in IE, FF, Opera & Chrome
1

The problem seems to be that this disabling is inherited. So, even though you aren't selected them in the $() they still get disabled. But this can also be in our favor.

After disabling, you can enable the inputs.

$('body').css('MozUserSelect', '-moz-none');
$('input').css('MozUserSelect', 'text');

NOTE: the value must be '-moz-none'. If 'none', it can't be changed.

I can't test IE nor do I have a solution for Opera. But maybe this will help part way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.