- Posted On 24 September 2014
- By
- In Programming
Hello friend, in this post I am going to share simple way of disabling Cut, Copy and Paste operation in input textbox using jQuery. It prevents user to perform any of these operations including shortcuts like Ctrl+C, Ctrl+X and Ctrl+V and right click too.
Many developers are not aware that there are events exist for each of this operation so they achieve it by detecting the keycode on keydown/up event and so on. This thing made me to share this information here so while reading other post you will come to know about this if not already known to you.
Let’s check the code first.
$(document).ready(function(){ $('#input1').bind("cut copy paste",function(event) { event.preventDefault(); }); });
That’s it. Above is the only code to restrict cut, copy and paste all together on textbox. Isn’t it cool?
For beginners, let’s discuss how the code is working.
As like jQuery tagline "Write Less, Do More" in above code I have bind multiple events together to the input having id “input1” and just called a action preventDefult() of event object in it.
event.preventDefault() is an action which prevents the default action to be triggered when called therefore nothing happens when any of the above event gets triggered.
If you want to know more about event.preventDefault() you can visit here.
You can say this solution as cross browser solution as its support almost all browsers if gets used for input element. I have tested it for IE8+, Firefox and Google chrome. For detailed compatibility you can check at this link at quirksmode.org.
Hope you have benefited this simple but useful code.
Feel the joy of sharing by any of the share button provided. Do not forget to share your comments too. Thanks.
Below is live fiddle of the above code.
- Tags :
- ASP.NET
- MVC
- jQueryJavascript
Top 10 Visual Studio things which can boost developers coding speed
Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.
Visual Studio 2008 Shell and TFS integration
Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
How to call click or any event only once in jQuery
Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.
Best CSS Gradient background generator tools online
Here are some best CSS gradient background code generator online tools using which you can create a cross browser css code for gradient backgrounds.