I have this right now:
<input type="text" placeholder="Paste text" onPaste="alert(this.value);">
This does infact work, except, it returns a blank alert window. I don’t get any value. Help?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
onpasteevent fires before theinput‘svalueis changed. You need something such as asetTimeout:I’m storing a reference to
thisinside a global var asthisis not accessible inside the scope of a timeout function which is attached to the window object.I’m using 4 miliseconds as the Timeout as it’s the minimum valid Interval/Timeout in the HTML5 specification. Edit: As noted in the comments, you may also use
0miliseconds as timeOut which is automatically recalculated to4. jsPerf tests.Fiddle
You may as well use a function call inside your
onpasteevent passingthisas a parameter to prevent your HTML mixing with JS too much. 🙂And here’s a function easier to read and which you can use in multiple inputs:
Which can be called with simply
onPaste="pasted(this)"for any input.Fiddle