JavaScript: copy content to clipboard code, javascript code

Source: Internet
Author: User

JavaScript: copy content to clipboard code, javascript code

Recently, a front-end project has been created, where you need to copy the values in the input or textarea directly to the Clipboard through the button. The following small series will share with you my Implementation ideas and code. You can directly introduce them to the project.

The Code is as follows:

function copyToClipboard(elem) {// create hidden text element, if it doesn't already existvar targetId = "_hiddenCopyText_";var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";var origSelectionStart, origSelectionEnd;if (isInput) {// can just use the original source element for the selection and copytarget = elem;origSelectionStart = elem.selectionStart;origSelectionEnd = elem.selectionEnd;} else {// must use a temporary form element for the selection and copytarget = document.getElementById(targetId);if (!target) {var target = document.createElement("textarea");target.style.position = "absolute";target.style.left = "-9999px";target.style.top = "0";target.id = targetId;document.body.appendChild(target);}target.textContent = elem.textContent;}// select the contentvar currentFocus = document.activeElement;target.focus();target.setSelectionRange(0, target.value.length);// copy the selectionvar succeed;try {succeed = document.execCommand("copy");} catch(e) {succeed = false;}// restore original focusif (currentFocus && typeof currentFocus.focus === "function") {currentFocus.focus();}if (isInput) {// restore prior selectionelem.setSelectionRange(origSelectionStart, origSelectionEnd);} else {// clear temporary contenttarget.textContent = "";}return succeed;}

We can directly call this method as follows:

copyToClipboard(document.getElementById("name"));

In this way, the value of id as name enters the clipboard.

I will introduce the JavaScript implementation and copy content to the clipboard code editor here. I hope it will be helpful to you!

Articles you may be interested in:
  • JS copies specific content to clipboard
  • JavaScript code for copying to clipboard
  • How to copy image addresses to clipboard by clicking images in js

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.