js copy to clipboard cross browser

/** copy text to the clipboard */
function copy2clipboard( text, callback /* optional */ ) {

	// use modern clipboard API
	if ( navigator.clipboard ) {
		navigator.clipboard.writeText( text )
		.then( function(){
			// if a callback is provided, call it
			callback && callback();
		}).catch( function( err ){
			errorMessage( err );
		}); 
	}
	// use old document.execCommand('copy')
	else {

		// create a temporary textArea containing the text
		var textArea = document.createElement( 'textarea' );
		textArea.setAttribute( 'style', 'width:1px;border:0;opacity:0;' );
		document.body.appendChild( textArea );
		textArea.value = text;

		// select the textArea
		textArea.select();

		try {

			// copy from textArea
			var isCopied = document.execCommand('copy');

			// if copy was successful, and a callback is provided, call it. if copy failed, display error message
			isCopied ? ( callback && callback() ) : errorMessage();

		} 
		catch( err ) {
			errorMessage( err );
		}
		// remove temporary textArea
		document.body.removeChild( textArea );
	}

	/** display error message */
	function errorMessage( err ) { 
		alert( 'Copy to clipboard failed ' + ( err || '' ) ) 
	};
    
}

3.78
9
Awgiedawgie 440215 points

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

<p id="text">Hello</p>
<button onclick="copyToClipboard('#text')"></button>

3.78 (9 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 to clipboard using javascript with one line copy text from input to clipboard javascript how to copy to clipboard in js how to copy to clipboard in javascript copy text with javascript how to copy text in js copy text to clipboard js get text from clipboard js copy a static value to clipboard js how to copy to clipboard on click js javascript copy to clipboard from variable javascript print clipboard content copy the text in javascript copy to clipboard text js copy to clipboard js how to copy clipboard in javascript javascript code to copy text to clipboard add to paste clipboard javascript programmatically copy to clipboard with js how to use clipboard in javascript js set to clipboard show content of clipboard with javascript javascript copy large text to clipboard copy string to clipboard javascript script copy text to clipboard how to access clipboard in javascript js paste from clipboard javascript copy stuff to your clipboard add text to clipboard javascript js copy to clipboard variable how to get clipboard text in js copy text javascript copy div text to clipboard in javascript put copy text in javascipt how to copy text with javascript javascript copy text to clipboard how to copy text using javascript javascript copy to clipboard without formatting javascript copy to clipboard selected js copy text to clipboard function copy text js js copy to clipboard easiest js copy string to clipboard paste from clipboard javascript js copy to clipboard on click copy text to clipboard typescript java script copy to clipboard how to copy text in js? js copy span text to clipboard javascript copy text to clipboard without input access to clipboard javascript javascript save to clipboard copy text in javascript copy a text in javascript copy clipboard javascript how to add text to clipboard event javascript how to copy text into clipboard using javascript javascript copy to clipboard javascript add to clipboard copy text javaxript js put into clipboard js put custom text to clipboard javascript put to clipboard how to copy clipboard in js javascript copy selected text to clipboard how to copy text with javascipt g javascript copy to clipboard copy string to clipboard javascript when i press span copy text in clipboard javascript copy text js javascript copy text by id to clipboard how to add something to clipboard javascript javascript copy string to clipboard copy to clipboard javascript copy to clipboard javascript copy to clipboard javascript javascript copy to clipboard site:stackoverflow.com js set clipboard Copy-to-clipboard bootstrap volt add copy option html javascript copy stringn in clipboard copy to clipboard html qwith css how to create copy link in html javascript copy data to clipboard how to create a list and copy to clipboard using javascript clipboard copy text Write a JavaScript program to copy a string to the clipboard. recente html javascripts copy past copy in clipboard in jquery how to copy button data copy a text from a button to an input field how to copy text to clipboard on a website paste image vom clipboard javascript how to take copytext in js copy an input text into clipboard How to copy things to clipboard with javascript copy variable to keyboard javascript write copy into document js copy to clipboard web text clipboard compy paste html code input button javascript write in clipboard inline copy link to clipboard jquery copy in clipbord in angulerjs php text copy button that copies to clip board js cjs copy to clipboard example of copy to clipboard clipboard.js example change value clipboard javascript copy to clipboard javascript from div javascript copy to clipboard string javascript copy formatted text to clipboard js copy with javascript how to copy a string to clipboard js handle copy on page how to copy text in javascript button copy to clipboard execCommand('copy') on text tap text to copy css js text to clipboard copy input text copy button example how to copy text to clipboard using javascript copy on click javascript copy url to clipboard jquery with tooltip javascript prompt copy to clipboard javascript copy to clipboard without input js copy to clipboard cross browser copy to the clipboard javascript type out clipboard js html copy button code w3c select box copy to clipboard how to add copy button using javascript copy and paste button html how to copy js get data copied to clipboard js copy text on click jquery copy-to-clipboard input value copy text button html button to duplicate text in html copy image on button click html javascript text copy script copy to clipboard code example copy Text. js clipboad.js copy button alert html copy paragraph in dom to clipboard copy to clipboard icon html copy taext boxex in html put text in clipboard javascript html coppy html code to have plusbutoon and shown copy same fields tag to copy text copy in clipboard javascript how to copy some text to clipboard how to copy text in the clipboard in js html5 copy text how to preovide a copy clipboard for html click link to copy text html copy link to clipboard cliboard js php javascript copy text to clipboard how to replace user clipboard in js copy text from input js how to put something in user clipboard using js create copy link button how to give auto copy by click with html how to use clipboard javascript without downloading it click text to copy to clipboard table html html copy link onclick js send text to clipboard set clipboard js copied text button onclick jquery copy input text to clipboard clipboard.js copy textarea html clipboard-copy clipboard i html copy text to clipboard button html link copy to clipboard javascript how to make a button that paste text in a textbox html html copy tag php code to copy a text on onclick javascript library to add copy button js push button to clipboard paste javascript copy text js function set clipboard javascript copy link javascript button cop html bootstrap copy php copy input field html to clipboard copy button in html not working how to create the copy butto in html copy data to clipboard on click html @copy html how to copy to clipboard with button js php create copy button php copy button implement copy to clipboard button javascript copy link using icon function copy text in html copy pre made string to clipboard javascript without input copy link button in html copy button bootstrap copy box html copy text from div to clipboard html5 bootstrap 4 js clipboard automatically copy html html input fields with copy button how to make a copy button in html5 html copy to clipboard onclick copy to clipboard jquery add elements t clipnord js how to copy html code with javascript copy input text to php copy code button javascript programmatically copy to clipboard javascript button to copy url js how to create a clipboard in javascript how to copy coupon code when clicking html js copy to clip copy link to clipboard javascript html copy to clipboard on click html copy to clipboard button add a copy button html copy html page to clipboard copy to clipboard javascript copy text field to clipboard javascript set clipboard javascript js copy to clipboard copy to clipboard button bootstrap copy click html javascript set value in copy buffer copy text to clipboard 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