Open In App

How to disable Copy, Paste, Cut and Right Click using jQuery ?

Improve
Improve
Like Article
Like
Save
Share
Report

The ctrl+c, ctrl+v, ctrl+x and a right-click of the mouse is used to copy, paste and cut anything from anywhere. It can be disable for particular task or page. Let see how to disable cut, copy, paste and right-click.

It can be done in two ways:

  • By using an on() method
  • By using keydown() and mousedown() method

By using an on() method: It is a built-in method in jQuery. With the help of this method, we will able to disabled the cut, copy, paste and right-click option.

  • Syntax:
    $(“selector”).on(event, function)
  • Example:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>The jQuery Example</title>
        <script src=
        </script>
        <style>
            #geek {
                padding: 65 px 0;
            }
        </style>
      
        <script>
            $(document).ready(function() {
                
                // Disables ctrl+v, ctrl+x, ctrl+c.
                $('textarea').on("cut", function(e) {
                    $("#d2").text('cut. not allowed!');
                    e.preventDefault();
                });
                $('textarea').on("copy", function(e) {
                    $("#d2").text('copy. not allowed!');
                    e.preventDefault();
                });
                $('textarea').on("paste", function(e) {
                    $("#d2").text('paste. not allowed!');
                    e.preventDefault();
                });
                
                // Above all three can be combined into one, above is 
                // executed separately for understanding purposes.
                /* $('textarea').on("cut copy paste", function(e) {
                $("#d2").text('right-click is disabled!');
                e.preventDefault();
                }); */
                
                // Disables right-click.
                $('textarea').mousedown(function(e) {
                    if (e.button == 2) {
                        e.preventDefault();
                        alert('right-click is disabled!');
                    }
                });
            });
        </script>
    </head>
      
    <body>
        <center>
            <div id='geek'>
                <h1 style="color:green">GeeksforGeeks</h1>
                <p id="d1">
                    The below textarea won't allow any cut, copy, 
                    paste and right-click operations.
                </p>
                <textarea></textarea>
                <p id="d2" style="color:red"></p>
            </div>
        </center>
    </body>
      
    </html>

    
    

  • Output:

By using keydown() and mousedown() method: By using the key-down event and for disabling right-click we use mouse-down() method. With the help of these two methods, we will able to disabled the cut, copy, paste and right-click option.

  • Syntax:
    $(“selector”).keydown(function)
    $(“selector”).mousedown(function)
  • Example:




    <!DOCTYPE html>
    <html>
      
    <head>
        <script src=
        </script>
        <style>
            #geek {
                padding: 65px 0;
            }
        </style>
      
        <script>
            $(document).ready(function() {
                $(document).keydown(function(event) {
      
                    // 86 is the keycode of v
                    if (event.ctrlKey == true && (event.which == '86')) {
                        $("#d2").text('paste. not allowed!');
                        event.preventDefault();
                    }
      
                    // 88 is the keycode of x
                    if (event.ctrlKey == true && (event.which == '88')) {
                        $("#d2").text('cut. not allowed!');
                        event.preventDefault();
                    }
      
                    // 67 is the keycode of c
                    if (event.ctrlKey == true && (event.which == '67')) {
                        $("#d2").text('copy. not allowed!');
                        event.preventDefault();
                    }
      
                    // Above all three can be combined into one, above is 
                    // executed separately for understanding purposes.
                    /* if (event.ctrlKey==true && (event.which == '86' 
                    || event.which == '67' || event.which == '88')) {
                        alert('cut. copy. paste. not allowed!');
                        event.preventDefault();
                    } */
                });
                $('textarea').mousedown(function(e) {
                    if (e.button == 2) {
                        alert('right-click is disabled!');
                        e.preventDefault();
                    }
                });
            });
        </script>
    </head>
      
    <body>
        <center>
            <div id='geek'>
                <h1 style="color:green">GeeksforGeeks</h1>
                <p id="d1">
                    The below textarea won't allow any cut, copy,
                    paste and right-click operations.
                </p>
                <textarea></textarea>
                <p id="d2" style="color:red"></p>
            </div>
        </center>
    </body>
      
    </html>

    
    

  • Output:


Last Updated : 28 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads