Run command Ctrl+V "paste" with mouse click inside an input

Asked

Viewed 1,272 times

2

Hello friends it is possible with a mouse click to run the command Ctrl + v "paste" in an input with jquery?

i already have a script that copies everything inside the input. now I need another that when clicking on another input paste the copied text.

I was able to make it work on IE but not on Chrome


function paste() {
    document.execCommand('paste')
}


  • 1

    Mouse click where? On the element, on a button? It would be good to [Dit] the question and give as many relevant details as possible, so that the question can be answered objectively. Here are some tips: [Ask].

  • ok amended question..

  • If you want to access the Clipboard is possible, however, unreliable regarding compatibility. But if you’re just copying and pasting from the same page, you could just save it to a variable.

4 answers

2

  • Junior Oliveira, first of all, welcome to SOPT! .

  • Thanks Junior for the answer. yes it worked more just in ie like function in Chrome

0

That should work:

function paste() {
    document.execCommand('paste')
}

$(function(){
    $('input[type="text"]').focus(function(){
        paste();
    });
});
  • unfortunately not working in Chrome browser.

  • Does the JS stop working whenever there is an error, checked if there is any on the page before executing this code? or even if there is some error in the execution of this?

  • actually just left this code on the page for testing which alias worked perfectly in IE.. more here in the company we use Chrome. );

  • I understand, but you need to run this page with the Chrome console open, not to be working, almost sure there is some error, this error I need to know to identify what is wrong, Knowing that it doesn’t work on Chrome isn’t enough to know why. You know how to see on Chrome console if there is an error on the page?

  • opened the console but nothing appears. let me ask. you managed to make this script work?

0

Okay, I used it like this on my website.

<script type="text/javascript">
    var copyTextareaBtn = document.querySelector('.textarea');

    copyTextareaBtn.addEventListener('click', function(event) {
      var copyTextarea = document.querySelector('.textarea');
      copyTextarea.select();

      try {
        var successful = document.execCommand('copy');
        var msg = successful ? 'sim!' : 'não!';
      } catch (err) {

      }
    });
 </script>



    <textarea class="textarea"  rows="4" cols="25"  onClick="this.setSelectionRange(0, this.value.length)" >
        <a  target="_blank" href="http://www.adota-me.tk">
            <img border="0" alt="adota-me.tk"  src="https://scontent-mad1-1.xx.fbcdn.net/hphotos-xpt1/v/t1.0-9/12688280_1755291221366035_3011947917400908476_n.jpg?oh=894ed3c1dc9994d4ab489aab424844ff&oe=575423D6" width="150" height="100">
        </a>
    </textarea>
  • Amadeu thanks for the answer. but it’s not what I need. I already have a script that copies everything inside the textarea. now I need another that when clicking on another textarea paste the copied text.

-1

You need a click method to call CRTL V.

For example:

$("p").click(function(){
//aqui faz a função});

Do a search on jQuery . keypress in this documentation here.

This jQuery forum here can also help you.

A hug.

  • 1

    Matheus thanks for the reply.. Plus I think I didn’t express right I edited the question. hug...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.