본문 바로가기

개발자정보

Javascript Copy to clipboard 개발방법

반응형
HTML 소스
<input type="text" class="form-control" id="fullSql" name="fullSql" value="ApexClass : '<?=$profile[class_name]?>'">
<button type="button" id="btn_copy" name="btn_copy"  class="btn btn-outline-primary mb-1" onclick="fn_sqlCopy();">
  <i class="fa fa-check"></i> 복사
</button>
자바스크립트 소스
  <script>
	function fn_sqlCopy() {
      var obj = document.saveFrom.fullSql;
	  obj.select();
      obj.setSelectionRange(0, 99999);

	  try {
		var successful = document.execCommand('copy');
		var msg = successful ? 'Copied!' : 'Failed copy...';
	} catch (err) {
		console.log('Error occurs...');
	}

	obj.setSelectionRange(0, 0);

	  // alert(document.saveFrom.fullSql.value);
	  // navigator.clipboard.writeText(document.saveFrom.fullSql.value);
	}
  </script>
반응형