There are a few similar questions to this but none quite the same.
I want to know if there is an event that can be used to execute some JS before a page is submitting (i.e. POSTed).
But yes, it is an option.
Working example Just wondering... is it possible that the form will be submitted before the hidden field gets updated? Wouldn't it be wise to first prevent the submission, then change the hidden field, and first then submit it? When using a click() event on the button, yes it is.
To call and run a JavaScript function from an HTML form submit event, you need to assign the function that you want to run to the onsubmit event attribute. By assigning the test() function to the onsubmit attribute, the test() function will be called every time the form is submitted.
Something like this?
<form onsubmit="do_something()"> function do_something(){ // Do your stuff here }
If you put return
like the code below, you can prevent the form submission by returning false from the do_something()
function.
<form onsubmit="return do_something()"> function do_something(){ // Do your stuff here return true; // submit the form return false; // don't submit the form }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With