Saturday, December 17, 2016

Capture text pasted into / cut from a text field with JQuery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Capture text pasted into / cut from a text field with JQuery</title>
    <script src="jquery-3.1.1.min.js"></script>
    <style type="text/css">
        #txt_complaint {
            width: 300px;
            margin: 10px;
            height: 30px;
            left: calc(50% - 150px);
            position: absolute;
        }
    </style>
</head>
<body>

<input type="text" id="txt_complaint"/>

<script type="text/javascript">
    $("#txt_complaint").bind('paste', function(e) {
        var elem = $(this);

        setTimeout(function() {
            var text = elem.val();
            alert("Value=" + text);
        }, 10);
    });

    $("#txt_complaint").bind('cut', function(e) {
        var elem = $(this);

        setTimeout(function() {
            var text = elem.val();
            alert("Value=" + text);
        }, 10);
    });
</script>

</body>
</html>

No comments:

Post a Comment