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

161
Views
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 answers
Answer question

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 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!