简体   繁体   中英

How do i copy text to clipboard - cross browser

Ok so feels like i have gone down a rabbit hole of how to copy text to clipboard on here and tried a lot of suggestions

seems easy to do it for chrome but that option doesn't work in other browsers

I have a few requirements

  • I would like to copy text to clipboard
  • to be able to copy a section of html with multiple elements
  • To work in safari and chrome at least
  • Vanilla Javascript

I have found this solution and it works except that it copies the html tags as well?

i tried changing the.innerHTML to.value on the button, but that comes back undefined


<div id="something">
  <div>first name: <span class="name">name</span></div>
  <div>Job title: <span class="job">job</span></div>
    <div>Phone number: <a href="0123456789" class="number">0123456789</a></div>
  <img class="companylogo" src="./img/example.jpg">
</div>


<button onclick="copyToClipboard(document.getElementById('something').innerHTML)">
  Copy the stuff
</button>


<script>

    /* copy function */
function copyToClipboard(textToCopy) {
  var textArea;

  function isOS() {
    //can use a better detection logic here
    return navigator.userAgent.match(/ipad|iphone/i);
  }

  function createTextArea(text) {
    textArea = document.createElement('textArea');
    textArea.readOnly = true;
    textArea.contentEditable = true;
    textArea.value = text;
    document.body.appendChild(textArea);
  }

  function selectText() {
    var range, selection;

    if (isOS()) {
      range = document.createRange();
      range.selectNodeContents(textArea);
      selection = window.getSelection();
      selection.removeAllRanges();
      selection.addRange(range);
      textArea.setSelectionRange(0, 999999);
    } else {
      textArea.select();
    }
  }

  function copyTo() {
    document.execCommand('copy');
    document.body.removeChild(textArea);
  }

  createTextArea(textToCopy);
  selectText();
  copyTo();
}

</script>

document.getElementById('something').innerHTML

sends html code inside #something to copyToClipboard and this behavior you mentiond is expected. what do you want to be copied to clipboard if not html?

thanks Hosseind600 for trying to help

but i managed to find some code that does exactly what i would like it to do. whilst ticking off the requirements i had set

its currently the second answer but highest votes How to copy text from a div to clipboard

   <html>
        <body>
            <div id="a" onclick="copyDivToClipboard()"> Click to copy </div>
            <script>
                function copyDivToClipboard() {
                    var range = document.createRange();
                    range.selectNode(document.getElementById("a"));
                    window.getSelection().removeAllRanges(); // clear current selection
                    window.getSelection().addRange(range); // to select text
                    document.execCommand("copy");
                    window.getSelection().removeAllRanges();// to deselect
                }
            </script>
        </body>
    </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM