Disable Enable Deselect Text Selection using JavaScript jQuery

Thursday, September 22, 2011

As our subscriber request, how to Disable Enable Deselect Text Selection using JavaScript jQuery let us see how to work with prevent / disable text selection in web page using JavaScript / jQuery,



Here is the solution to prevent or disable enable text selection based on our requirement.

We can do the following:with this script:


Disable Text selection on your web page
Enable Text Selection on particular Area
Deselect Selected Text if you are in protected Area.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Disable /Prevent Text Selection jQuery-JavaScript</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
// Begin:
// Jquery Function to Disable Text Selection
(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)
// EO Jquery Function

// Usage
// Load Jquery min .js ignore if already done so
// $("element to disable text").disableTextSelect();
// $("element to Enable text").enableTextSelect();
//

jQuery(function($) {
$("body").disableTextSelect();

$("#code").mouseover(function(){
 $("body").enableTextSelect();
});

$("#code").mouseout(function(){  

// Code for Deselect Text When Mouseout the Code Area
if (window.getSelection) {
  if (window.getSelection().empty) {  // Chrome
    window.getSelection().empty();
  } else if (window.getSelection().removeAllRanges) {  // Firefox
    window.getSelection().removeAllRanges();
  }
} else if (document.selection) {  // IE?
  document.selection.empty();
}
       
 $("body").disableTextSelect();
});
});
</script>

<style type="text/css">
<!--
body {
 margin-left: 10px;
 margin-top: 10px;
}

#code
{
 padding:20px;
 border:2px dashed #CCC;
 font-family:"Courier New", Courier, monospace;
 font-size:15px;
 background-color:#EEE;
}
-->
</style>
</head>

<body>
<h2>Disable /Prevent Text Selection jQuery-JavaScript</h2>
<p>Below this Code Area User can Allow to Select a Text in other Area text selection was disabled</p>
<p id="code">
$(".elementsToDisableSelectFor").disableTextSelect();</p>
<p>When you are leaving above code box selection was deselected! If selected !</p>
</body>
</html>


Download This Script     Live Demo

No comments:

Post a Comment