Contents

Develop
2018.04.05 09:57

[js] Click button copy to clipboard

Views 4402 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

Example no jQuery.


# JAVASCRIPT

function copyToClipboard(elementId) {

  // Create a "hidden" input
  var aux = document.createElement("input");

  // Assign it the value of the specified element
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);

  // Append it to the body
  document.body.appendChild(aux);

  // Highlight its content
  aux.select();

  // Copy the highlighted text
  document.execCommand("copy");

  // Remove it from the body
  document.body.removeChild(aux);

}


# HTML

<p id="p1">P1: I am paragraph 1</p>
<p id="p2">P2: I am a second paragraph</p>
<button onclick="copyToClipboard('p1')">Copy P1</button>
<button onclick="copyToClipboard('p2')">Copy P2</button>
<br/><br/><input type="text" placeholder="Paste here for test" />



[출처] https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery




?

List of Articles
No. Category Subject Author Date Views
1129 Develop [android] AlertDialog 메시지 창 띄우기 hooni 2015.07.09 2058
1128 Develop GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스 file hooni 2014.10.14 2066
1127 Develop [c#] mfc 기반의 웹서비스 서버/클라이언트 샘플과 예제 소스 secret hooni 2013.04.23 2073
1126 Develop [js] 스크롤 이벤트 막기 hooni 2015.04.14 2074
1125 Develop [ios] Thread Loop 내에서 UI 업데이트 방법 hooni 2015.01.03 2075
1124 Develop [linux] CentOS Apache 웹서버에 HTTPS 적용 hooni 2015.10.23 2089
1123 Develop [c#] MS IE(Internet Explorer) 툴바 버튼 예제 2003/2005 두가지 버전 secret hooni 2013.04.23 2090
1122 Develop [maven] Mac OS에 메이븐(maven) 설치하기 file hooni 2015.01.21 2095
1121 Develop 정리할 자료. file hooni 2015.07.02 2099
1120 Develop XE Core 1.8.18 본문 작성시 태그(html) 사라지는 버그 file hooni 2016.04.21 2110
1119 Develop [ios] ViewController Push할 때 애니메이션 효과 hooni 2015.10.23 2119
1118 Develop [ios] 최상위 ViewController 가져오기 hooni 2015.10.12 2125
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 99 Next
/ 99