Search code examples
javascripthtmlformsfunctionsend

function and getElementById not working in Javascript


I have created a page where if the input length is 0, it shows an alert. And if input length is not 0, it does the function that href"mailto:[email protected]" property does(Did it with document.getElementByID). I want that after clicking on Send button, it sends an email or does the function that Html's href"mailto:[email protected]" property does. But it is not working. The code is:

HTML:

<form action="index.html">
   <input type="submit" class="btn" value="Send" onClick="auth(event)">
   <div>
     <a href="mailto:[email protected]" id="SendMail"></a>
   </div>
</form>

JavaScript:

    function auth(event) {
  event.preventDefault();

  var username = document.getElementById("username").value;

  if (username.length === 0) {
    alert("Please enter your email");
    return;
  } else if (username.length === 5) {
    document.getElementById("SendMail")
  } 
  }

Solution

  • You're just taking the ID reference and doing nothing with it. I think what you want to do is in this answer: https://stackoverflow.com/a/271172/2938055