4
4
4eloBek2015-09-28 21:51:17
JavaScript
4eloBek, 2015-09-28 21:51:17

Why is text not being written to a variable from the buffer when it is pasted on the phone?

There is this:
> Example on jsfiddle
HTML :

<input type="text" maxlength="0" placeholder="Paste your text">
<textarea id="txt" disabled></textarea>

css :
body {text-align: center;}

input[type=text] {
    display: block;
    margin: 30px auto;
    border: 1px dashed #3d3d3d;
    text-align: center;
    outline: none;
}
textarea {
    position: relative;
    width: 250px;
    outline: none; 
    resize: none;
    margin-left: -5px;
}

JS :
document.querySelector("input").addEventListener("paste", function(e) {
            alert("Thanks")
      e.preventDefault();	
      var pastetext = e.clipboardData.getData("text/plain");
      document.getElementById('txt').value = pastetext;
    });

Works on a computer. On the phone - no(( The function
is called because alert is displayed , but the variable is not written to. What
's the problem? )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivanq, 2015-09-28
@4eloBek

Most likely you don't have access to clipboardData. But there is a workaround - the content of the clipboard is in the input. And maxlength must be removed.
Full code:
HTML

<input id="txt2" type="text" placeholder="Paste your text">
<textarea id="txt" disabled></textarea>

JS
document.querySelector("#txt2").addEventListener("paste", function(e) {
            alert("Thanks")
      e.preventDefault();	
      var pastetext = e.clipboardData.getData("text/plain");
      document.getElementById('txt').value = document.getElementById('txt2').value;
    });
document.querySelector("#txt2").addEventListener("change", function() {
      document.getElementById('txt2').value = '';
    });

I take out the purification in a separate event, since letters can also be entered from the keyboard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question