Copy To Clipboard | Sololearn: Learn to code for FREE!

+3

Copy To Clipboard

hi guys does anyone know how to make a button to copy a p tag inner html ? (this is the code i wrote but it doesnt work) <button id="clipboardBtn" onclick="copyToClipboard()">Copy to Clipboard</button> js: function copyToClipboard(element) { /* Get the text field */ let copyText = document.getElementById("result"); /* Select the text field */ copyText.select(); copyText.setSelectionRange(0, 99999); /* For mobile devices */ /* Copy the text inside the text field */ navigator.clipboard.writeText(copyText.value); /* Alert the copied text */ alert("Copied the text!"); }

8/10/2022 9:58:00 AM

Abolfazl Abbaspour

4 Answers

New Answer

+3

As I understand it, the select() function method is not used on the <p> element, so you need to create an <input> element, or a <textarea> element and assign the <p> text to it, and only then copy it.

+2

This is a really good example i found: https://code.sololearn.com/W1EsB8qiZWKH/?ref=app

+2

🍇 Alex Tușinean 💜 yes but in this example, she copie from a input tah not a P tag

+2

Solo tnx i try it out