How to Disable Text Selection using JQuery?
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 - ItSolutionStuff.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>
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 - ItSolutionStuff.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 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 - ItSolutionStuff.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>
<script type="text/javascript">
$('body').disableSelection();
</script>
</body>
</html>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- JQuery Get Days Difference Between Two Dates Example
- How to Disable Submit Button After Form Submission in PHP?
- JQuery Check If Value Exists or Not in Array Example
- How to Split String to Array By Comma using JQuery?
- How to Only Allow Numbers in a Text Box using jQuery?
- How to Disable F5 Refresh Button using JQuery?
- How to Disable Browser Back Button using JQuery?
- JQuery Rotate Image Animation Example Code
- How to Remove Duplicate Object from Array in JQuery?
- How to Add Lazy Loading Image using JQuery?
- How to Remove Comma from String in JQuery?
- JQuery Add Remove Input Fields Dynamically Example
- How to set vertical align middle div by CSS in HTML?
- How to set vertical align middle div by CSS in HTML?