0

I'm trying to develop a function that can manipulate pasted text before it reaches the textbox.

My current issue is that no data is gathered when this code is ran in Firefox.

document.getElementById("myTextBox").addEventListener('paste', (e) => {
    console.log(e.clipboardData.getData('text')) 
  });

From my testing this only works in Chrome and Edge. In Firefox it returns a empty string.

My Firefox version is 115.02 (64bit)

I have also viewed most of the questions on Stack Overflow that pertain to this question.

Ive tried bitwise OR'ing like this (window.clipboardData||e.clipboardData)

This didnt work on Firefox either.

EDIT:

Thanks to all those that answered but none of the solutions work so I will be attaching a codepen to recreate the issue

https://codepen.io/Jimmy-Gore/pen/VwVmzVV?editors=1111

This pen demonstrates the issue, as no text will show up in the console.

2 Answers2

0

Try OR again, but with switched values:

document.getElementById("myTextBox").addEventListener('paste', (e) => {
    console.log((e.clipboardData || window.clipboardData).getData('text')) 
});

Hope this helps!

MfyDev
  • 439
  • 1
  • 12
0

Try attaching the event listener to window object instead of the actual element:

window.addEventListener('paste', e => {
    console.log((e.clipboardData || window.clipboardData).getData('text')) 
});
MfyDev
  • 439
  • 1
  • 12