How To Disable Text Selection in HTML Using CSS & jquery?


0


In this tutorial, we will show you how to disable text selection in html using css on website page with jquery. We will discuss about disable content selection using CSS and jquery. now we will give you more examples to disable text selection on a web page with jquery and CSS. on the website page with jquery

We will give you an easy example of disable text selection on website Jquery CSS properties. You just need to just add some CSS on your HTML body tag that you can see in the bellow example code. You can simply copy and try that.

Example 1 – Disable text selection on web page in HTML using CSS

<!DOCTYPE html>
<html>
<head>
   <title>How to disable text selection on html web page using JQuery & CSS? - NiceSnippets.com</title>
   <style type="text/css">
        body{
            user-select: none; /* supported by Chrome and Opera */
            -webkit-user-select: none; /* Safari */
            -khtml-user-select: none; /* Konqueror HTML */
            -moz-user-select: none; /* Firefox */
            -ms-user-select: none; /* Internet Explorer/Edge */
        }
   </style>
</head>
<body>
    <h3>How to disable text selection on html web page using JQuery?</h3>
  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

Next example, We will give you easy core jquery. We can generate our own disableSelection () jqurey function and we will use it on HTML body tag. So you can also check below code for that.

Example 2 – disable selection of text on web page using JQuery

<!DOCTYPE html>
<html>
<head>
   <title>How to disable text selection on html web page using JQuery & CSS? - NiceSnippets.com</title>
   <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
    <h3>How to disable text selection on html web page using JQuery?</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <script type="text/javascript">
        (function($){
            $.fn.disableSelection = function() {
                return this
                         .attr('unselectable', 'on')
                         .css('user-select', 'none')
                         .on('selectstart', false);
            };
        })(jQuery);
        
        $('body').disableSelection();
    </script>
</body>
</html>

In this example, we will use jquery ui. as we know jquery ui provide multiple function that awesome. in this code, we will use disableSelection() for disable text selection on website jquery, so let’s see below code:

Example 3 – disable text selection on website jquery ui

<!DOCTYPE html>
<html>
<head>
   <title>How to disable text selection on html web page using JQuery & CSS? - NiceSnippets.com</title>
   <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
   <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
    <h3>How to disable text selection on html web page using JQuery?</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        $('body').disableSelection();
    </script>
</body>
</html>

Have Fun!


Like it? Share with your friends!

0
Developer

0 Comments