Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
HTML Form Not Working When Calling SetTimeout in the onsubmit Attribute

I do not have much experience with, especially with dealing with the client-side and I'm struggling when dealing with a form submission after the user clicks on the 'Send Message' button.

Here is the code snippet for the form:

<form onsubmit="sendMail()" method="post" id="contactForm" target="_blank">
    <label for="fullName">Full Name: </label><br>
    <input type="text" name="fullName" id="fullName" required><br>

    <label for="phoneNumber">Phone Number: </label><br>
    <input type="text" name="phoneNumer" id="phoneNumber" required><br>

    <label for="eMail">Email: </label><br>
    <input type="email" name="eMail" id="eMail" required><br>

    <label for="emailTitle">Subject: </label><br>
    <input type="text" name="emailTitle" id="emailTitle" required><br>

    <label for="userMessage">Message: </label><br>
    <textarea name="userMessage" id="userMessage" required></textarea><br>

    <button id="formSubmit" type="submit">Send Message</button>
</form>

Ideally, the sendMail() function would display a thank you message by modifying the DOM and then I would like it to wait for about 3 seconds before using the mailto happens. The issue is that the delay happens when I have target="_blank" on the form, but when I remove it, then the page simply refreshes. Any idea of why this would be happening?

Here's the code for the sendMail function.

function sendMail(){
    
    getFeedback(); 
    setTimeout(() => {
        let emailSubject = document.getElementById('emailTitle').value;  
        let emailBody = `${document.getElementById('fullName').value}\n${document.getElementById('phoneNumber').value}\n${document.getElementById('eMail').value}\n${document.getElementById('userMessage').value}`;
        document.location.href = `mailto:customersupport@sidekick.edu?subject=${encodeURIComponent(emailSubject)}&body=${encodeURIComponent(emailBody)}`;
    }, 3000);
    
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try adding the event and preventDefault() to your function, that way, the form don't get sent the normal way.

function sendMail(e){
    e.preventDefault();
    getFeedback(); 
    setTimeout(() => {
        let emailSubject = document.getElementById('emailTitle').value;  
        let emailBody = `${document.getElementById('fullName').value}\n${document.getElementById('phoneNumber').value}\n${document.getElementById('eMail').value}\n${document.getElementById('userMessage').value}`;
        document.location.href = `mailto:customersupport@sidekick.edu?subject=${encodeURIComponent(emailSubject)}&body=${encodeURIComponent(emailBody)}`;
    }, 3000);
    
}

You might also want to pass the event to your html <form> tag.
<form onsubmit="sendMail(event)">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!