Rabu, 02 Maret 2016

How To Detect A Copy, Paste, And Cut Event Using jQuery

To detect a copy, paste, and cut event in jQuery, you can use bind function.

Example


Following the jQuery snippet code using bind function to detect a copy, paste, and cut event in the textfield.

$(document).ready(function() {
    $("#textfield").bind({
        copy : function(){
        $('#message').text('copy event detected!');
        },
        paste : function(){
        $('#message').text('paste event detected!');
        },
        cut : function(){
        $('#message').text('cut event detected!');
        }
    });
}); 

Complete code:

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <style type="text/css">

        span{
            color:red;
            font-weight:bold;
        }

        </style>

    </head>

    <body>

        <h1>Detect Cut, Copy and Paste with jQuery</h1>

        <form action="#">
            <label>Text Box : </label>
            <input id="field1" type="text" size="60" value="Please Copy, paste or cut any text here" />
        </form>

        <span id="message"></span>

        <script type="text/javascript">

        $(document).ready(function() {
            $("#field1").bind({
                copy : function(){
                $('#message').text('copy event detected!');

                },

                paste : function(){
                $('#message').text('paste event detected!');
                },

                cut : function(){
                $('#message').text('cut event detected!');
                }

            });

        });

        </script>
    </body>
</html>