is there a way to make a button in HTML that when every time you click it, a new pre-filled email will pop up?

Viewed 42

I am trying to create a website where I have this button and every single time you click it, a new pre-filled email will pop up. I know how to link the email, but is there a way I can do it so that every time I click the button, it's a random email I already pre-filled?

This is my code right now:

    <li><a href="mailto:?"><i class="fa fa-envelope"></i></a></li>
var diffEmails = [
    // (code deleted off here because 
    // I didn't want to show the email/pre-filled email body for privacy reasons)
];

    function randomEmails() {
        var i = parseInt(Math.random() * diffEmails.length);
        location.href = diffEmails[i];
    }
<a href="mailto:" >onclick="randomEmails();"></a>

I used a social media icon from font awesome and don't want to delete it so how could I use this in my HTML

1 Answers

You need a random number:

var emails = [
  'a@a.com',
  'b@b.com',
  'c@c.com',
  'd@d.com',
  'e@e.com',
  'f@f.com'
]


pickEmail = function() {
  let new_email;
  if (emails.length == 0) {
    // or you will have to generate new emails, and
    // push them into `emails`
    new_email = 'No email available.';
  } else {
    let idx = Math.floor((Math.random() * emails.length));
    // make sure this email will not be poped up again.
    new_email = emails.pop(idx);
  }
  document.getElementById('email').innerHTML = new_email;
}
<button onclick="pickEmail()">
Random Email
</button>

<p>
Email
</p>
<p id="email">

</p>

Related