Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alert after paste event in Javascript?

Tags:

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?

like image 517
Googlebot Avatar asked Dec 15 '12 18:12

Googlebot


People also ask

How do you paste text in JavaScript?

Use navigator. clipboard to get access to the clipboard. Use writeText() to copy text into the clipboard. Use readText() to paste the text.


1 Answers

You could put your alert in a setTimeout.

setTimeout(function() {alert('Pasted');}, 0); 

This will delay the code until after the value has updated.

Just keep in mind that this in the setTimeout callback will have a different value than that in the enclosing environment.

If you'll need a reference to the outer this, which will be the element, then reference it in a variable...

var self = this; setTimeout(function() {alert(self.value);}, 0); 

Or use .bind() (Supported in most browsers that support addEventListener. Older Safari didn't support it.)...

setTimeout(function() {alert(this.value);}.bind(this), 0); 
like image 144
I Hate Lazy Avatar answered Oct 14 '22 20:10

I Hate Lazy