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

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 Report

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 Report

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