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

198
Views
Stopping page from refreshing when submitting a form

I'm trying to write some code that takes an input, but I'm having trouble because when the form is submitted the page refreshes.

This is my code:

const changeColour = () => {
  console.log("Submitted");
  const colourForm = document.getElementById("testForm");
  const newColour = colourForm.querySelector("input").value;
  console.log(newFinish);
};

const newForm = vehicle.querySelector(".vehicle-colour");
newForm.addEventListener("submit", changeFinish);

In the console log I get Submitted and the value of newColour, which is my desired output, but it's only there for a split second and then the page refreshes. I'm not too sure how to prevent the page from refreshing.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Calling the .preventDefault method of the event fired should stop the page from submitting.

change

newForm.addEventListener("submit", changeFinish);

to

newForm.addEventListener("submit", event => {
  event.preventDefault();
  changeFinish(event);
});
about 4 years ago · Juan Pablo Isaza Report

0

changeFinish must be a function with "event" as argument.

const changeFinish = event => {
   event.preventDefault();
   /* code here */
}
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!