javascript - ClipboardJS : Copy the content of a <code> section - Stack Overflow

I need to copy the content of a <code><code> section using ClipboardJS. When following the

I need to copy the content of a <code></code> section using ClipboardJS. When following the tutorial, I end up with this error :

Uncaught Error: Invalid "target" value, use a valid Element

Any workaround on this ?

EDIT:

HTML

<code id="#foo">my fantastic code</code>
    <button class="copy-button" data-clipboard-action="copy" data-clipboard-target="#foo">
      Copy !
</button>

JAVASCRIPT

<script type="text/javascript">
  var clipboard = new Clipboard('.copy-button');

  clipboard.on('success', function(e) {
      console.info('Action:', e.action);
      console.info('Text:', e.text);
      console.info('Trigger:', e.trigger);

      e.clearSelection();
  });

  clipboard.on('error', function(e) {
      console.error('Action:', e.action);
      console.error('Trigger:', e.trigger);
  });
</script>

I need to copy the content of a <code></code> section using ClipboardJS. When following the tutorial, I end up with this error :

Uncaught Error: Invalid "target" value, use a valid Element

Any workaround on this ?

EDIT:

HTML

<code id="#foo">my fantastic code</code>
    <button class="copy-button" data-clipboard-action="copy" data-clipboard-target="#foo">
      Copy !
</button>

JAVASCRIPT

<script type="text/javascript">
  var clipboard = new Clipboard('.copy-button');

  clipboard.on('success', function(e) {
      console.info('Action:', e.action);
      console.info('Text:', e.text);
      console.info('Trigger:', e.trigger);

      e.clearSelection();
  });

  clipboard.on('error', function(e) {
      console.error('Action:', e.action);
      console.error('Trigger:', e.trigger);
  });
</script>
Share Improve this question edited Oct 12, 2021 at 1:20 Rifky Niyas 2,0761 gold badge14 silver badges29 bronze badges asked Feb 23, 2017 at 17:52 user4187476user4187476 3
  • Where is your actual code? It looks like you aren't selecting the element correctly. You can see here that it works fine on code elements. – Mike Cluck Commented Feb 23, 2017 at 17:53
  • @MikeC Thank you for your input. I edited with my code – user4187476 Commented Feb 23, 2017 at 18:00
  • 3 You have a typo. It should be <code id="foo">..., not <code id="#foo">... – Mike Cluck Commented Feb 23, 2017 at 18:01
Add a ment  | 

1 Answer 1

Reset to default 7

Try to change this line:

<code id="#foo">my fantastic code</code>

for

<code id="foo">my fantastic code</code>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745668347a4639252.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信