copy innerHLM of div using clipboardjs

Hi everybody,
I need to copy the content of the div to the desktop when clicked the div using https://clipboardjs.com/

<div id="id1" onclick="copy_content_to_clipboard(this);">
Textttttt
</div>

Anybody can help me?
Thank you.
LVL 1
myyisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
here we go - no need to do the onclick handler:
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.5/clipboard.min.js"></script>
  </head>

  <body>
    <!-- 1. Define some markup -->
    <div id="myCopySource" style="border: 2px solid" data-clipboard-action="copy" data-clipboard-target="#myCopySource"><h1>Inner Heading</h1><br/><span>And a span is also in here.</span></div>
    <!-- 2. Instantiate clipboard -->
    <script>
    var clipboard = new Clipboard("#myCopySource");
    clipboard.on('success', function(e) {
        console.log(e);
    });
    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>
  </body>

</html>

Open in new window


Live sample:
http://embed.plnkr.co/vrqHBavtmDLLgqTWMtUe/

HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
myyisAuthor Commented:
Thank you very much for your quick answer. I have 2 questions:

1. How is it working for  "onclick" without an onclick script, if I want to make it onmouseover, what shall I do?

2.  At the examples here https://clipboardjs.com/ when you click the copy button you see a small confirmation pop-up. How can turn this on?
Rainer JeschorCommented:
Hi,
1: thats because the constructor implements the onclick listener.
For an onmouseover you might track that event and fire a click event on the same element. But not sure if this makes sense (from an user perspective).

2: Because they implemented additionally a showTooltip function
showTooltip(e.trigger, 'Copied!');

Open in new window

but only on buttons with the class "btn".

HTH
Rainer
myyisAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.