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

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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