Search code examples
javascripthtmlscriptingdynamic-html

How to copy text from a paragraph elements from JavaScript?


Hey am a new developer and am trying to build a website. The main goal of website is to provide a piece of text to the visitors. Some of my helpers gave me a javascript to copy the text inside a <p></p> element. But it's not working. The text inside the<p></p> is not copied when the button is clicked.Somehow if you know the answer please use my constant, veriable and other values. My Javascript

 var buttons = document.getElementsByClassName('copystatus');

for (let button of buttons) {
  button.addEventListener('click', function() {
     let statusElement = this.closest('.latestatus');
     let textToCopy = statusElement.getElementsByClassName('copytxt')[0].innerHTML;
    
    copyTextToClipboard(textToCopy);
    addCopyStatusAlert(this.parentNode);
  });
}

function copyTextToClipboard(text) {
  const copyText = document.createElement('textarea');
  copyText.style.position="absolute";
  copyText.style.display="none";
  copyText.value = text;

  document.body.appendChild(copyText);
  copyText.select();
  document.execCommand('copy');
  document.body.removeChild(copyText);
}

function addCopyStatusAlert(element) {
  if (!element.getElementsByClassName('status-copy-alert').length) {
    let copyAlertElement = document.createElement('span');
    copyAlertElement.classList.add('status-copy-alert')
    let copyMessage = document.createTextNode('Copied!');
    copyAlertElement.appendChild(copyMessage);

    element.appendChild(copyAlertElement);
    setTimeout(function() {
      element.removeChild(copyAlertElement);
    }, 700);
  }
}

My Html

<div class="mainStatus">
   <h2 class="statusHeading">Latest English Status</h2>
   <div class="allStatus">
    <div class="bock">
     <div class="latestatus">
      <p class="copytxt">Life is good when you have books</p>
      <div>
       <button class="copystatus btn">Copy</button>
      </div>
     </div>
     <div class="latestatus">
      <p class="copytxt">Google is an open source library by Larry Page and Sergey Brin!</p>
      <div>
       <button class="copystatus btn">Copy</button>
      </div>
     </div>
    </div>
    <div class="block">
     <div class="latestatus">
      <p class="copytxt">Cats are better than dogs.</p>
      <div>
       <button class="copystatus btn">Copy</button>
      </div>
     </div>
     <div class="latestatus">
      <p class="copytxt">Cats are better than dogs.</p>
      <div>
       <button class="copystatus btn">Copy</button>
      </div>
     </div>
    </div>
   </div>
  </div>

Please refine the code so that it could copy the text inside <p></p> element when respective button is clicked.

And additionally can anyone modify the code above so that I don't need to give repeatedly class for <p></p>.(Optional)


Solution

  • You need the text to be painted to the screen so it can be selected, try removing copyText.style.display="none";, or replacing it with another approach, ex:

    copyText.style.left="-99999px";
    copyText.style.zIndex="99999";