Reputation: 6471
When the user pastes anything and no input/textarea is focused on the page, I want the paste event to be handled by a specific default textarea
. How do I do this?
Upvotes: 0
Views: 204
Reputation: 207501
So listen for paste event. Determine if it is in an input. If it is not, read the clipboard data and do something with it.
window.addEventListener("paste", function (evt) {
if (!['TEXTAREA', 'INPUT'].includes(evt.target.nodeName)) {
const clipboardData = evt.clipboardData || window.clipboardData
const pastedData = clipboardData.getData('Text')
var ta = document.querySelector("textarea")
ta.value = ta.value + pastedData
}
})
<p>Hello</p>
<textarea></textarea>
<input />
<p>Hello</p>
Upvotes: 1