top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to disable text selection using jQuery?

+2 votes
344 views
How to disable text selection using jQuery?
posted Mar 16, 2015 by Muskan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

In JQuery 1.8+ try the following -

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

You can also try the following -

(function($) {
    if ($.browser.mozilla) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'none'
                });
            });
        };
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : ''
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('selectstart.disableTextSelect', function() {
                    return false;
                });
            });
        };
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).unbind('selectstart.disableTextSelect');
            });
        };
    } else {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('mousedown.disableTextSelect', function() {
                    return false;
                });
            });
        };
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).unbind('mousedown.disableTextSelect');
            });
        };
    }
})(jQuery);

Src: http://code.jdempster.com/jQuery.DisableTextSelect/jquery.disable.text.select.js

answer Mar 16, 2015 by Salil Agrawal
Similar Questions
+5 votes

I need disable all refresh events

Example.

  1. F5,
  2. Mouse right click option (refresh/reload),
  3. CTRL+R,
  4. Menu bar -> view -> refresh and
  5. URL refresh/reload button

How to disable these all events by using javascript...

Advance thanks....

+5 votes

I am using a linear layout and I want to position my EditText in center and linear layout full width.
This is my code,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="#eeeeee">

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="50px"
    android:background="#666666">

        <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/newEvent"
        android:hint="Enter the Name of Event"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true" />

    </LinearLayout>
</RelativeLayout>
...