클릭시 복사버튼 여러개

클릭시 복사버튼 여러개

QA

클릭시 복사버튼 여러개

본문

게시판 리스트 페이지에 버튼 클릭하면 여분필드 wr_7의 텍스트가 복사대는 것을 구현하였습니다.

근데 리스트페이지다보니깐 버튼이 여러개인데 하나만.. 복사 기능이 되더라구요

혹시 버튼이 여러개라서 해당 여분필드 값이 각각 복사되게 어떻게 할수 있을까요?

 


<div class="tag">
                        <div id="instaTag">
                        <?php echo $view['wr_7'] ?>
                        </div>
                        <button onclick="copy_to_clipboard()">copy</button>
                    </div>    
                        
                    <script>
                        function copy_to_clipboard() {    
                          var copyText = document.getElementById('instaTag').textContent;
                          var textArea = document.createElement('textarea'); 
                          
                          document.body.appendChild(textArea); 
                          textArea.value = copyText;
                          textArea.select(); 
                          document.execCommand('copy');
                          document.body.removeChild(textArea);
                          alert('복사되었습니다.');
                        }

이 질문에 댓글 쓰기 :

답변 1


<button onclick="copy_to_clipboard('<?=$view['wr_7']?>')">copy</button>


<script>
  function copy_to_clipboard(txt) {    
    var copyText = txt;
    var textArea = document.createElement('textarea'); 
    
    document.body.appendChild(textArea); 
    textArea.value = copyText;
    textArea.select(); 
    document.execCommand('copy');
    document.body.removeChild(textArea);
    alert('복사되었습니다.');
  }
</script>


이건 제가 독해하기 쉬운 바닐라군요.
textarea 를 생성시켰다가 카피가 끝나면 소멸시켜 버리네요.
저는 display none 과 block 을 오가는 식으로 하는데...
그래서 한수 배우고 갑니다.ㅋ

답변을 작성하시기 전에 로그인 해주세요.
전체 2,632
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT