Open In App

How to Disable Text Selection using jQuery?

Last Updated : 12 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to disable text Selection using jQuery. Disabling text selection refers to preventing users from highlighting or selecting text content on a webpage. Text se­lection is a common feature in numerous web applications. However, there might arise situations where the prevention of users from selecting and copying text content on a web page is desire­d. In such cases, jQuery, an exte­nsively used JavaScript library, provides a convenient solution.

Syntax:

$(document).ready(function() {

// Disable text selection
$("body").css("-webkit-user-select", "none");
$("body").css("-moz-user-select", "none");
$("body").css("-ms-user-select", "none");
$("body").css("user-select", "none");
});

There are different approaches to Disable Text Selection Using jQuery. Let’s discuss each of them one by one.

  • Disable Text Selection for Specific Elements
  • Toggle Text Selection On and Off

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Disable Text Selection for Specific Elements

In this approach, jQuery is utilize­d to implement targete­d text selection control. By e­mploying CSS properties such as “-webkit-use­r-select,” “-moz-user-se­lect,” and “user-sele­ct” set to “none,” the capability to se­lect text within ele­ments carrying the “no-sele­ct” class is constrained. This effective­ly ensures that the conte­nt contained within those specific e­lements cannot be highlighte­d or copied.

Syntax:

$(document).ready(function () {

// Disable text selection for elements
// with class "no-select"
$(".no-select").css({
"-webkit-user-select": "none",
"-moz-user-select": "none",
"-ms-user-select": "none",
"user-select": "none"
});
});

Example: In this example, we are using jQuery to disabling text selection by applying CSS rules to elements with class “no-select”. The layout features centered content with a white background, green-bordered “no-select” div, and clean design.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Disable Text Selection Using jQuery</title>
    <script src=
    </script>
  
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
  
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin: 4rem;
        }
  
        .no-select {
            border: 3px solid green;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 10px;
            margin-bottom: 10px;
            color: green;
        }
  
        p {
            line-height: 1.6;
        }
    </style>
</head>
  
<body>
    <div class="container">
        <h1>
            Disable Text Selection Using jQuery
        </h1>
        <div class="no-select">
            <h1>
                Welcome To Geeksforgeeks
            </h1>
        </div>
        <p>
            A Computer Science portal for geeks.
            It contains well written, well thought
            and well explained computer science
            and programming articles
        </p>
    </div>
  
    <script>
        $(document).ready(function () {
              
            // Disable text selection for elements
            // with class "no-select"
            $(".no-select").css({
                "-webkit-user-select": "none",
                "-moz-user-select": "none",
                "-ms-user-select": "none",
                "user-select": "none"
            });
        });
    </script>
</body>
  
</html>


Output:

How-To-Disable-Text-Selection-Using-Jquery-Example-1

Approach 2: Toggle Text Selection On and Off

This approach introduces a dynamic fe­ature using jQuery to enable­ or disable text sele­ction throughout the entire page­. The state is toggled by clicking a button. Whe­n text selection is allowe­d, the default behavior re­mains unchanged. However, whe­n it is disabled, the same CSS prope­rties are applied to the­ “body” element, pre­venting text sele­ction across the entire page­.

Syntax:

$(document).ready(function () {
let allowTextSelection = true;

$("#toggle-button").click(function () {
allowTextSelection = !allowTextSelection;

if (allowTextSelection) {
$("body").css("user-select", "auto");
} else {
$("body").css("user-select", "none");
}
});
});

Example: This example­ demonstrates a webpage­ that employs jQuery to enable­ users to easily switch text se­lection. The design showcase­s a centered containe­r with a maximum width of 600px, a heading GeeksforGe­eks, Furthermore­, the “Toggle Text Se­lection” button is styled with a dynamic blue background that intensifies on hover.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Toggle Text Selection</title>
  
    <script src=
    </script>
  
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
  
        .container {
            max-width: 600px;
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
  
        h1 {
            margin-top: 0;
        }
  
        p {
            line-height: 1.6;
        }
  
        #toggle-button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }
  
        #toggle-button:hover {
            background-color: blue;
        }
    </style>
</head>
  
<body>
    <div class="container">
        <h1>Welcome To GeeksforGeeks</h1>
        <p>
            A Computer Science portal for geeks.
            It contains well-written, well-thought,
            and well-explained computer science
            and programming articles.</p>
        <button id="toggle-button">
            Toggle Text Selection
        </button>
    </div>
  
    <script>
        $(document).ready(function () {
            let allowTextSelection = true;
  
            $("#toggle-button").click(function () {
                allowTextSelection = !allowTextSelection;
  
                if (allowTextSelection) {
                    $("body").css("user-select", "auto");
                } else {
                    $("body").css("user-select", "none");
                }
            });
        });
    </script>
</body>
  
</html>


Output:

How-To-Disable-Text-Selection-Using-Jquery-Example-2

How To Disable Text Selection Using jQuery



Similar Reads

How to disable text selection highlighting using CSS?
Making text unselectable is an easy task. All we have to do is to disable the selectivity of the text on our webpage for all the browsers that the webpage is likely to be loaded on. There are some settings/commands used in CSS which are used for enabling/disabling functionalities on specific browsers. Like to disable a certain functionality on our
1 min read
How to Disable Text Selection in ReactJS ?
In this article, we will see how to disable the text selection in ReactJS. Prerequisites:Introduction to ReactReact HooksNPM or NPXWeb applications ofte­n provide a valuable feature­ called text sele­ction, enabling users to easily highlight and copy content from a webpage. However, there are situations where disabling text sele­ction becomes neces
4 min read
How to disable the modification of text canvas using Fabric.js ?
In this article, we are going to see how to disable the modification of text canvas using FabricJS. The canvas means text written is movable, rotatable, resizable, and can be stretched. But in this article, we will disable the modification. Further, the text itself cannot be edited like a textbox.Approach: To make it possible, we are going to use a
2 min read
How to change text selection color in the browsers using CSS ?
Most of the browsers by default highlight the selected text in a blue background. This can be changed by using the ::selection pseudo selector in CSS. The ::selectionselector is used for setting the CSS property to the part of the document that is selected by the user (such as clicking and dragging the mouse across text). Only some CSS properties a
1 min read
How to create Disable flip toggle switch using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Disable flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.
1 min read
How to disable HTML links using JavaScript / jQuery ?
Given an HTML link and the task is to disable the link by using JavaScript/jQuery. Approach 1: Disable HTML link using JavaScript using setAttribute() Method. JavaScript setAttribute() Method: This method adds the defined attribute to an element and gives it to the passed value. In case of the specified attribute already exists, the value is set/ch
5 min read
How to disable scroll to change number in &lt;input type="number"&gt; field using JavaScript/jQuery?
Given an HTML document containing &lt;input type = "number"&gt; attribute and the task is to disable the mouse scroll wheel to change the number value using JavaScript/jQuery. Approach: Select the element.Add an event to that element which will call a function when scrolling starts.Make the element disabled on scrolling.Example 1: This example impl
2 min read
How to disable dragging an image from an HTML page using JavaScript/jQuery ?
Drag and Drop is a very interactive and user-friendly concept which makes it easier to move an object to a different location by dragging it. It allows the user to click and hold the mouse button over an element, drag it to another location, and release the mouse button to drop the element there. In HTML 5 Drag and Drop are much easier to code and
2 min read
How to enable/disable all input controls inside a form element using jQuery ?
Give an HTML document containing a form element. The form element contains input elements, option elements, and button elements. The task is to enable or disable all input controls inside a form using jQuery. It can be done by using prop() method. Using prop() Method: It is used to set or return the properties and values for the selected elements.
3 min read
How to disable Copy, Paste, Cut and Right Click using jQuery ?
The ctrl+c, ctrl+v, ctrl+x and a right-click of the mouse is used to copy, paste and cut anything from anywhere. It can be disable for particular task or page. Let see how to disable cut, copy, paste and right-click. It can be done in two ways: By using an on() method By using keydown() and mousedown() method By using an on() method: It is a built-
3 min read