How to disable text selection on html web page using JQuery & CSS?

Admin   Jquery   70  2020-08-10 10:54:35

Here, we will discuss how to disable text selection on website page using jquery. we can also disable content selection using css and jquery. here i will give you more examples to disable text selection on a web page using jquery and css.

I given first i will give you simple example of disable text selection on website jquery using css property. you need to just add some css on your body tag as you can see on bellow example example code. you can simply copy and check that.

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

<!DOCTYPE html>

<html>

<head>

<title>disable selection of text on web page - </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>

Second example i will give you simple core jquery. we can create our own disableSelection() jqurey function and we will use it on body tag. so you can also check bellow code for that.

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

<!DOCTYPE html>

<html>

<head>

<title>disable selection of text on web page - </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 Third Example, we will use jquery ui. as we know jquery ui provide several function that amazing. in this example we will use disableSelection() for disable text selection on website jquery, so let's see bellow code:

Example 3 - disable text selection on website jquery ui

<!DOCTYPE html>

<html>

<head>

<title>disable text selection on website jquery - </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>

<script type="text/javascript">

$('body').disableSelection();

</script>

</body>

</html>

I hope it can help you...