1

I need to copy my e-mail address to a clipboard if user has no email client defined. i found a solution somewhere on the web:

html:

<input type="text" value="someemail@gmail.com" id="myInput" style="display:none";>

js:

  function copyEmail() {

  var copyText = document.getElementById("myInput");

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999);

  /* Copy the text inside the text field */
  document.execCommand('copy');

  /* Alert the copied text */
  alert("You don't have an email client defined"  + '\n' + "Email address copied: " + copyText.value + ".");
}

  $('a[href^=mailto]').each(function() {
    var href = $(this).attr('href');
    $(this).click(function() {
      var t;
      var self = $(this);

      $(window).blur(function() {
        // The browser apparently responded, so stop the timeout.
        clearTimeout(t);
      });

      t = setTimeout(function() { copyEmail() }, 1);

    });
  });

it gets the value, but it doesn't copy it to the clipboard. What am I doing wrong?

DuchSuvaa
  • 539
  • 1
  • 5
  • 22
  • https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery I think it will help you. –  Dec 17 '19 at 12:34
  • i tried everything in the provided link, but only Nadav's simplified version works. still i would be happy to know what i was doing wrong – DuchSuvaa Dec 17 '19 at 12:50
  • whats more it only works with button click, which is not what i was trying to acheive – DuchSuvaa Dec 17 '19 at 13:01

0 Answers0