특정문자를 Clipboard에 복사붙여넣기

5o_hyun·2022년 7월 21일
0

Clipboard에 특정 값을 복사시키는 법


function clickHandeler(){
	const data = "이걸 복사하고 싶어요"; // 2

	let tempInput = document.createElement("input"); // 1
	tempInput.value = data; // 3


	document.body.appendChild(tempInput); // 4
	tempInput.select(); 
	document.execCommand("copy"); // 5
	document.body.removeChild(tempInput); // 6
};

<img src="" alt="" onClick="clickHandeler();">
  1. 입력이 가능한 element를 생성

  2. 복사할 값을 변수에 넣는다.

  3. 변수를 element.value에 담는다.

  4. body 에 element를 자식노드로 추가한다. (나중에 삭제할거라 body 가 아니여도됨)

  5. select() 후 execCommand("copy")로 복사한다.

  6. 복사를 마쳤으니 처음 만든 element를 제거한다.

profile
학생 점심 좀 차려

0개의 댓글