+ 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!"); }

10th Aug 2022, 9:58 AM
Abolfazl Abbaspour
Abolfazl Abbaspour - avatar
4 Answers
+ 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.
10th Aug 2022, 11:39 AM
Solo
Solo - avatar
+ 2
This is a really good example i found: https://code.sololearn.com/W1EsB8qiZWKH/?ref=app
10th Aug 2022, 10:56 AM
šŸ‡ Alex Tușinean šŸ’œ
šŸ‡ Alex Tușinean šŸ’œ - avatar
+ 2
šŸ‡ Alex Tușinean šŸ’œ yes but in this example, she copie from a input tah not a P tag
10th Aug 2022, 10:57 AM
Abolfazl Abbaspour
Abolfazl Abbaspour - avatar
+ 2
Solo tnx i try it out
10th Aug 2022, 11:50 AM
Abolfazl Abbaspour
Abolfazl Abbaspour - avatar