Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda