how to enable click copy function using js

var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

copyTextareaBtn.addEventListener('click', function(event) {
  var copyTextarea = document.querySelector('.js-copytextarea');
  copyTextarea.focus();
  copyTextarea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }
});

////html code below
<p>
  <button class="js-textareacopybtn" style="vertical-align:top;">Copy Textarea</button>
  <textarea class="js-copytextarea">Hello I'm some text</textarea>
</p>

5
1
Awgiedawgie 440215 points

                                    function copyToClipboard(value) {
  navigator.clipboard.writeText(value)
}

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to enable click copy function using js javascript on click link copy how to add click to copy in javascript js on click copy copy to clipboard copy to clipboard when clicked link href copy to clipboard click to copy in javascript on click copy text js code js click copy js click copy text when click make a copy js make a copy when click js copy to clipboard websites click link copy to clipboard javascript copy on click js click copy code click copy js js click to copy copy from clipboard how to copy on click javascript js button click to copy copy text from variable to clipboard how to copy on click js click to copy html without javascript copy to clipboard js copy on click click to copy js javascript copy text on click How to triger a function when some one try to copy some thing copy string on click in javascript click to copy javascript js copy text on click execCommand('copy') copy link to clipboard javascript js copy to clipboard document.execCommand('copy'); (1 liked) how to make copy text with html click copy javascript copy on click js create copy button for documentation html javascript click to copy urxvt copy to clipboard javascript copy string to clipboard TLabel copy to clipboard javascript .execcommand( copy ) text after copied js clipboard set text javascript put in clipboard make a copy button JS how to write program in javascript to show in clipboard make a copy button in javascript how to copy a string to clipboard in javascript html set clipboard data how to copy text using javascript js window.clipboard on click show copied in js copy link to clipboard html input copy button javascript setting clipboard how to copy and paste text in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source