given a content editable div. How can I detect a paste event, prevent the paste from being inserted so can I can intercept and sanitize the paste to include text only?
I also don’t want to lose focus after the paste + sanitize is complete.
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.
UPDATE:
All major browsers now give you access to the clipboard data in a paste event. See Nico Burns’s answer for an example on newer browser and also check out Tim Down’s answer if you need to support older browsers.
You can listen for the onPaste event on the div to detect the paste. If you just want to disable the paste you can call
event.preventDefault()from that listener.To capture the pasted content however is a little bit more difficult since the
onPasteevent does not give you access to the pasted content. The usual way to handle this is to do the following from theonPasteevent handler:setTimeout(sanitize, 0)and from your sanitizing method: