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

164
Visualizações
onClick event Submits form automatically

I have a form on my page and a button with id "calculate" in the form. When I press the button it will do some calculations using a function. But when I press the button it behaves like a submit button and sends the data. I don't want to send the form, I just want to calculate.

<form method="post" action="" enctype="multipart/form-data" id="form_add_data">
...
<button class="btn btn-info" id="calculate" onclick="calculate()"> Calculate </button>
...
</form>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Inside calculate function use:

function calculate(event){
    event.preventDefault()
    return false;
}

Prevent default will stop event propagation, and return false will keep the page from being refreshed.

about 4 years ago · Juan Pablo Isaza Relatório

0

You should use Event.preventDefault().

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.

In your situation:

document.getElementById("calculate").addEventListener("click", function(event){
  event.preventDefault()
});

or:

// in your JS file
function calculate(event){
      event.preventDefault()
}

read more about it here on MDN

about 4 years ago · Juan Pablo Isaza Relatório

0

type="button" is the answer as you said.

<button type="button" class="btn btn-info" id="calculate" onclick="calculate()"> Calculate </button>
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