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

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

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 Relatório

0

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

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