Imad
Imad

Reputation: 7490

Can't get value on paste event

I want to check pasted text in textbox to validate it but it is always blank.

How can I get text during while user paste it?

function alertandclear(obj) {
console.log('->' + obj.value);
obj.value = "";
}
<input type="text" onpaste="setTimeout(alertandclear(this),100)"/>

Upvotes: 0

Views: 1590

Answers (2)

sandy
sandy

Reputation: 303

You have to pass a function reference or expression inside the setTimeout.link. Here is what I did.

document.getElementById("myInput").addEventListener("paste",function(){
          var element = this;
          setTimeout(function(){console.log(element.value);element.value='';},100,element);
   });
<input id="myInput" type="text" />

Upvotes: 0

Jagdish Idhate
Jagdish Idhate

Reputation: 7742

You are calling setTimeout(alertandclear(this),100) instead of you should have reference to function in setTimeout as below

function alertandclear(obj) {
console.log('->' + obj.value);
obj.value = "";
}
<input type="text" onpaste="setTimeout(alertandclear.bind(null,this),100)"/>

Upvotes: 3

Related Questions