copy clipboard with span

document.getElementById("cp_btn").addEventListener("click", copy_password);

function copy_password() {
    var copyText = document.getElementById("pwd_spn");
    var textArea = document.createElement("textarea");
    textArea.value = copyText.textContent;
    document.body.appendChild(textArea);
    textArea.select();
    document.execCommand("Copy");
    textArea.remove();
}

0
0
Awgiedawgie 440215 points

                                    <span id="to-select-text">1111 - 2222 - 3333 - 4444</span>
<button id="copy-button">Copy</button>

0
0
4
3
Awgiedawgie 440215 points

                                    // Copy to clipboard on a click event
document.querySelector("#copy-button").addEventListener('click', function() {
	var reference_element = document.querySelector('#to-select-text');

	var range = document.createRange();  
	range.selectNodeContents(reference_element);

	window.getSelection().addRange(range);

	var success = document.execCommand('copy');
	if(success)
		console.log('Successfully copied to clipboard');
	else
		console.log('Unable to copy to clipboard');

	window.getSelection().removeRange(range);
});

4 (3 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
js copy div content to clipboard cpoy text to clipboard js js copy to clipboard div copy div text to clipboard typescript how to put a spam with the text "copy to clipboard" in js js click to copy text how to copy text in span javascript javascript copy div html content to clipboard js copy text element how to copy text to clipboard of tag p in javascript copy div content to clipboard javascript copy text to clipboard javascript javascript copy div content to clipboard copy span value to clipboard copy element to clipboard javascript javascript how to copy text javascript copy text copy to clipboard with spacing js javascript copy as plain text copy text to clipboard javascript without input copy span tag in cilipboard js copy span tag in cilipboard JavaScript copy text to clipboard without select javascript copy text in div to clipboard javascript copy text to clipboard without input how to copy text in clipboard javascript copy text js how to add text to clipboard evnt javascript clipboard text in span copy text to clipboard js js copy text javascript copy to clipboard from span element copy text javascript how to copy the text to the clipboard in javascript insert text to clipboard js js copy text to clipboard copy span text to clipboard javascript copy text inside javascript copy span text to clipboard copy text from clipboard javascript javascript copy div to clipboard copy span copy div text to clipboard js copy span to clipboard copy to clipboard without input span copy clipboard with span example copy clipboard with span on span click copy content copy span to clipboard javascript copy span to clipboard copy text from span javascript copy span text to clipboard jquery copy a span element to clipboard javascript code forr copy from span tag using javascript copied span on click copy to clipboard span tag copy to clipboard from span make copy of span js copy text from span js copy span text to clipboard javascript copy text to clipboard from span html copy from span how to copy textContent to clipboard in javascript copy textarea in reactjs copy to clipboard in reactjs
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