how to copy text from input through button click js

function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
}

3
2
Genius 95 points

                                    const span = document.querySelector(&quot;span&quot;);

span.onclick = function() {
  document.execCommand(&quot;copy&quot;);
}

span.addEventListener(&quot;copy&quot;, function(event) {
  event.preventDefault();
  if (event.clipboardData) {
    event.clipboardData.setData(&quot;text/plain&quot;, span.textContent);
    console.log(event.clipboardData.getData(&quot;text&quot;))
  }
});

3 (2 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 copy text from input through button click js js copy paragraph onclick click text to copy javascript javascript on click link copy onclick copy text javascript copy text in one click javascript js copy on click span js copy on click button to copy text javascript copy text when onClick button in js create element copy onclick click to copy element text javascript js copy text on click html onclick copy text copy text on click javascript copy on click javascript html input onclick copy js to add onclick to copy text how to add onclick to copy text copy button input html javascript click to copy javascript copy on click javascript onclick copy to clipboard how to copy text from when button is click copy button for input field copy html text on click onclick to copy input text value how can copy textcontent when click javascript input with copy button copy to clipboard javascript onclick html div onclick copy text javascript copy input text copy button inside input field in html copy button inside input field button input copy text html input button copy text create copy button in js html css code pen with text box input click to copy input value how to copy text from input on onclick copy a field text js onclick javascript how to copy text from input field how to copy specific text js button javascript copying text of button but onclick fires how i can create a copy button from input text in javascript add a button inside input to copy click to copy input text javascript how to copy a p tag text when i click a button in javascript copy value from input box to input id onclick js on click copy input javascript copy text on button click javascript copy input text with button in js copy input text with button how to copy text on button click in html copy text from input js copy text in input javascript copy input value oncick of button copy text button javascript copy text from input javascript copy text on click js copy button in input tag copy input value on click javascript click to copy input copy the text button in javascript javascript button to copy all inputs javascript button to copy different inputs
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