Copy paste in javascript

This is a centuries old question, but I still have problems with it. You see, I’m trying to insert some Excel data into the text area, but stupid browsers freeze for a long period of time when they do this, because God knows what kind of β€œsmart” parsing they do. I can’t get rid of this (uploading a file is out of the question, my boss wants me to insert rows from Excel into the text area).

The good news is that you insert into the standard WORKS text box. But I can not get them to insert there. So I am trying to catch the insert event in the text area, and then pass the text to the text box. Unfortunately, I settled on the insertion part. I cannot paste text through JS into a plain text field.

So my question is: how do you insert text, how do you call it through JS? There are some solutions that work only in IE, which is not very good, of course :: -).

+4
source share
5 answers

Sorry, I did not quite understand this idea. Can you connect to thextarea onpaste event (at least I know IE has such an event) and then just set the textarea value to the inserted value?

  pastedContent = window.clipboardData.getData("Text"); document.getElementById("yourtextarea").value = pastedContent; 

EDIT: OK, it looks like this only works in IE and newer versions of FF , but it is not a cross-browser solution.

+1
source

Simple

 var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") != -1 || navigator.userAgent.toLowerCase().indexOf("trident") != -1); document.getElementById('textinput').addEventListener('paste', function(e) { var text; if (isIe) { text = window.clipboardData.getData('Text') } else { text = e.clipboardData.getData('text/plain'); } // Do whatever you want with the text console.log(text); //If you don't want the text pasted in the textarea e.preventDefault(); }); 
 <textarea id="textinput"></textarea> 

If you want, you can even get rid of the text box and do it more directly. I wrote a technical blog post explaining how we copy and paste into Lucidchart (where I work).

+6
source

I cannot paste text via JS into a plain text field

When you say "plain text block", do you mean <input type="text"> ? If so, then I think setting its value attribute for the text you grabbed from <textarea> should work.

0
source

Enable javascript Copy to clipboard in Firefox or Mozilla: http://www.febooti.com/support/website-help/website-javascript-copy-clipboard.html

0
source

Try CodeMirror as an alternative solution. Does not check it with copy and paste with huge / large amount of data, but maybe this help ...

0
source

Source: https://habr.com/ru/post/1299471/


All Articles