'How to alert after paste event in Javascript?

I have a simple past event as

document.getElementById('paste_area').addEventListener('paste', function() {
    document.getElementById('notice').innerHTML='Text was successfully pasted!';
    alert('Pasted');
}, true);

A working example can be found here http://jsfiddle.net/XEQzz/

The alert and notice will appear before pasting. How can I delay the alert action to happen after paste event has been actually completed?



Solution 1:[1]

setTimeout seems the best option and you can use something like this to generalise

// object definition

    function PasteMonitor(element)
    {
        if(typeof element == "string")
        {
            this.target = document.getElementById(element);
        }
        else if(typeof element == "object" || typeof element == "function")
        {
            this.target = element;
        }

        if(this.target != null && this.target != undefined)
        {
            this.target.addEventListener('paste',this.inPaste.bind(this),false);
            this.target.addEventListener('change',this.changed.bind(this),false);
        }
        this.oldstate = "";
    }
    PasteMonitor.prototype = Object.create({},{
        pasted:{ value: false, enumerable: true, configurable: true, writable: true },
        changed:{ value: function(evt){
            //elements content is changed
            if(typeof this.onChange == "function")
            {
                this.onChange(evt);
            }
            if(this.pasted)
            {
                if(typeof this.afterPaste == "function")
                {
                    this.afterPaste(evt);
                    this.pasted = false;
                }
            }
        }, enumerable: true, configurable: true, writable: true },
        inPaste:{ value: function(evt){
            var cancelPaste = false;
            if(typeof this.beforePaste == "function")
            {
                // process pasted data
                cancelPaste = this.beforePaste(evt);
            }
            if(cancelPaste == true)
            {
                evt.preventDefault();
                return false;
            }
            this.pasted = true;
            setTimeout(function(){
                var evt = document.createEvent("HTMLEvents");
                evt.initEvent("change", false, true);
                this.target.dispatchEvent(evt);
            }.bind(this),0);
        }, enumerable: true, configurable: true, writable: true }
    })
    PasteMonitor.prototype.constructor = PasteMonitor;

//PasteMonitor usage

    //var element = document.getElementById('paste_area');
    var msgArea = document.getElementById('message');
    var myMonitor = new PasteMonitor("paste_area");
    //or use and object as argument => var myMonitor = new PasteMonitor(element);

    myMonitor.onChange = function(evt){
        if(this.pasted)
        {
            //do something on paste change
            msgArea.innerHTML = "onChange by paste";
            this.oldstate = this.target.value;
        }
        //check to prevent processing change event when the element loses focus if the change is done by paste
        else if(this.target.value != this.oldstate)
        {
            msgArea.innerHTML = "onChange by typing";
        }
    };
    myMonitor.afterPaste = function(evt){
       // do something after paste
        msgArea.innerHTML = "afterPaste";
    };
    myMonitor.beforePaste = function(evt){
        // do something before the actual paste
        msgArea.innerHTML = "beforePaste";
        //return true to prevent paste
        return false;
    };

Solution 2:[2]

For updating own value

 $(".number").on("paste", function (e) {
        var Value = e.originalEvent.clipboardData.getData('text');
        var Self=this
        setTimeout(function () {
            if (Value != Value.replace(/[^\d\.]/g, "")) {
                $(Self).val(Value.replace(/[^\d\.]/g, ""));
            }
        }, 0);
    });

Solution 3:[3]

I usually do like this to mimic an onafterpaste.

I have a special function called "onafterpaste" and I use it like this:

onpaste="onafterpaste(myFunction, this)"

As you see, it also accepts an extra parameter which will be used when myFunction is called.

See (and try it out in) the following live snippet:

function onafterpaste(f,elm) {
  setTimeout(function() {f(elm)},0);
}

function  myFunction(elm) {
  alert('After the paste, the input has the following text:\r\n\r\n'+elm.value);
}
<p>Paste something down here:</p>
<input type="text" onpaste="onafterpaste(myFunction,this)" value="The original text">

Solution 4:[4]

Is paste_area an input? input is also an event.

document.getElementById('paste_area').addEventListener('input', 
    function(event) {
        alert('Pasted');
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Atlantrix
Solution 2 VISHMAY
Solution 3 Magnus
Solution 4 Zamicol