Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
JavaScript Event Listener To Rediredct Page After Form Submission

I am trying to make my webpage redirect to a thank you page after a user submits my registration form.

Below is the javascript event listener that I have been trying to use but not working. Any help will be appreciated.

window.addEventListener("load", function() {
  const form = document.getElementById('submitForm');
  form.addEventListener("submit", function(e) {
    e.preventDefault();
    const data = new FormData(form);
    const action = e.target.action;
    fetch(action, {
      method: 'POST',
      body: data,
    })
    .then(() => {
      alert("https://www.google.com/");
    })
  });
});
   <div class='testbox'>
      <form action='https://script.google.com/macros/s/AKfycbyg4V7O2BTMG_u1QVROTCVhZlDo2viq3e2bcqjQ9X6ZBOCyBoQrkyJGK1Zw_gtFp6dw/exec' method='POST' enctype="multipart/form-data">
      
    <div class='name-item'>
            <input name='Surname' placeholder='Surname' required='' type='text'/>
            <input name='First Name' placeholder='First Name' required='' type='text'/>
             <input name='Middle Name' placeholder='Middle Name' required='' type='text'/>
           </div>
      
        
      <div class='question'>
          <center><p>Privacy Policy<span class='required'>*</span></p></center>
          <div class='question-answer checkbox-item'>
            <div>
              
              <center><label class='check' for='check_1'><span>By submitting this form you agree to the terms of service <a href='https://www.google.com/p/privacy-policy.html' target="_blank">privacy policy.</a></span></label></center>
            </div>
          </div>
        </div>
        <div class='btn-block'>
         <center> <button href='/' type='submit' id="submitForm">Submit</button></center>
        </div>
      </form>
    </div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const form = document.getElementById('submitForm');
<form action='...' method='POST' enctype="multipart/form-data">

Your <form> element does not have an id, so you cannot use getElementById to reference it.

You either need to add an id to your form:

<form action='...' method='POST' enctype="multipart/form-data" id="submitForm">

or use querySelector to reference it:

const form = document.querySelector("form");

NB: If you have multiple <form> elements in your page, querySelector will only return the first one.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda