1

Example

I have read that copying plain text from clipboardData to a content editable element isn't supported in Chrome on Android (like this post). When you run the example above on mobile, which aims to remove all formatting from pasted text, the text variable returns an empty string. This only happens in Chrome on Android. So I'd like to know if there is a workaround for that.

Example code modified from JavaScript get clipboard data on paste event (Cross browser)

$('#editableDiv').on('paste',function(e) { 
      e.preventDefault();
      var text;
      if( e.originalEvent.clipboardData ){
        text = (e.originalEvent || e).clipboardData.getData('text/plain');

        if(text === ''){

           console.log('Android Problem');  

        }

        document.execCommand('insertText', false, text);
      }
      else if( window.clipboardData ){
        text = window.clipboardData.getData('Text');
      if (window.getSelection)
        window.getSelection().getRangeAt(0).insertNode( document.createTextNode(text) );
      }
      console.log(text);
});

Updated: I have come up with a workaround by triggering a popup for users to paste text. Not ideal but could get the job done removing formatting. I'm looking for more workarounds/suggestions.

   if( e.originalEvent.clipboardData ){
    text = (e.originalEvent || e).clipboardData.getData('text/plain');
    if(text === ''){       
        text = prompt('Paste something..');  
    }
    document.execCommand('insertText', false, text);
  }
Community
  • 1
  • 1
RedGiant
  • 4,444
  • 11
  • 59
  • 146

1 Answers1

1
$(document).bind("paste", function (e) {
      text = e.originalEvent.clipboardData.getData('text');
      alert(text);
});
4b0
  • 21,981
  • 30
  • 95
  • 142
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Dec 30 '22 at 20:05
  • Users find it difficult to understand code only answers with no explanation. Please consider adding some description explaining how it solves the problem or add comments in the source code at appropriate places. – Azhar Khan Dec 31 '22 at 06:08