Copy and paste Javascript command

Asked

Viewed 1,721 times

5

I need to know how to give Ctrl c in the contents of a div when clicking on it. In this case it will have multiple Ivs, and it will take the contents of the div clicked.

I found some things on the internet but wanted something simple. Simply through the onclick pick up the content from the div.

It can be Javascript or Jquery. (Anyone serves, give it to work)

It doesn’t have to work on all browsers, just put the javascript command that copies (Ctrl + c). Because I believe it can help in the community.

Obs: I would like to be presented with a solution that is not using the technique of Clipboard

Grateful...

  • 1

    What exactly do you want to do with the copied content? You will use it on the same page or on another different page?

  • I don’t need to go into detail, I just want to copy the content.

  • I need a quick answer, so don’t bother telling me it’s duplicate. I need to use the Ctrl c command when you click a div. I was very specific. It is not copying content to another div or elsewhere, it is literally the Ctrl C command that is what makes the question different. Please answer my question. Don’t disturb the growing community.

  • 4

    This issue is being discussed at the goal: http://meta.pt.stackoverflow.com/q/4040/3635

  • 8

    What @Erloncharles means is that if it’s just the data transfer inside itself browser then there may be a simpler solution (no need to involve the clipboard). But if you want to paste from/to external applications, it’s more complicated. No one is trying to "mess with" you, no, it’s just the more context can give about your problem, better the ability of the community to help you. This attitude of yours is only hurting yourself.

  • 2

    @mgibsonbr is just that, this question is very interesting and better understand the need help in the resolution.

  • I’m going to use this to copy contents to an iframe. With ready answers and default. But I don’t want another solution.

  • And how does this command not work on all browsers? I want to know.

  • 1

    You want to copy the text from the div or the formatting as well?

  • I saw that you requested removal of the site publication, and I was wondering if there was any particular reason to request it. Because in fact, duplicates are welcome since created organically, just like yours was, so if that’s just the reason, it’s not necessary to remove it from the site.

  • That’s why I created another similar question. So this would be a duplicate of the duplicate. The other question I think is more organized. (This I wanted to exclude) Because the other one already has answer.

  • @Felipejorge It is good to keep duplicates on the site (as long as they are marked as duplicates), because they help other people to get to the relevant content. And this question came to receive a plausible answer, and with 3 votes already. We moderators chose to keep it, ok?

  • Okay, I just indicated to remove it because she was giving too much headache.

Show 8 more comments

1 answer

3

It’s kind of tricky, this whole copying thing clipboard something via javascript because it doesn’t work well in all browsers, one tactic is to make it easier for the user to copy. Example:

window.onload = function () {
  var divs = document.querySelectorAll('.copiar');
  
  for (var i=0; i<divs.length; i++) {
     divs[i].addEventListener("click", function () {
        prompt("Dê um CTRL + C para copiar e feche.", this.innerHTML);
     });  
  }
}
.copiar {
   border: 1px solid;
   margin: 30px 0;
   cursor: pointer;
}
<div class="copiar">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mauris mi, bibendum sed viverra in, maximus quis mauris. Aenean laoreet mattis orci, vel porta est. Maecenas vel nisi vitae felis auctor imperdiet. Morbi convallis dapibus leo. Nunc maximus ligula id felis porta, vitae tincidunt risus lobortis. Pellentesque mattis mauris at odio sagittis, vitae fermentum arcu ultricies. Donec in cursus enim, id pretium velit. Nunc elementum nunc augue, sed ullamcorper velit fermentum nec. Aliquam erat volutpat. Nulla erat diam, posuere cursus turpis imperdiet, luctus maximus nunc</div>

<a class="copiar">Praesent imperdiet nulla leo, a eleifend velit finibus sit amet. Donec sodales, orci a tincidunt varius, tellus eros dignissim sem, id lobortis neque lectus sed erat. Nam mattis risus et purus pharetra, vel mattis nisl tincidunt. Nunc ornare tempus mauris. Duis molestie volutpat dui eget facilisis. Aenean sagittis elit erat, at ultricies odio varius quis. Proin eu aliquam lacus. Proin et vestibulum leo. Sed porta ullamcorper sagittis. Phasellus commodo non felis nec rhoncus. Praesent placerat, nisl ut condimentum malesuada, ipsum elit pretium orci, eget interdum lacus sem vel libero. Nam dolor elit, malesuada eget rutrum non, efficitur at purus. Interdum et malesuada fames ac ante ipsum primis in faucibus</a>
  

If you want to try something more advanced: https://www.lucidchart.com/techblog/2014/12/02/definitive-guide-copying-pasting-javascript/

  • I can use this script as an alternative to other browsers, very good answer. But I want to put Ctrl c for browsers that allow this.

  • @FelipeJorge http://zeroclipboard.org/

Browser other questions tagged

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