I have a form which has a ‘submit’ button called Finish and a Javascript that runs when the Finish button is pressed which checks to make sure all mandatory fields have been completed which has been working well. We would like to add a 2nd ‘submit’ button to give users a choice of saving a draft or finalising the form and submitting that.
My problem is that when users click either the Save Draft or Finish button the Javascript runs. I haven’t been able to find out if it’s possible to only have this run when the Finish button is clicked and not when the Save Draft button is clicked.
Here’s the basics of my form and buttons/script:
$(window).load(function() {
$('#editRecord').on('submit', validate);
// code to check form valiation
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<form action="submit.php" method="post" class="" id="editRecord">
<div class="rate-buttons left py2">
<input type="submit" name="buttonType" value="Save Draft" class="mr1">
<input type="reset" name="buttonType" value="Reset" class="mx1">
<input type="submit" name="buttonType" value="Finish" class="ml1">
</div>
</form>
Is it possible to just have the Javascript run for a single submit button?
3
Answers
How can I get the button that caused the submit from the form submit event?
The above link has a solution that uses JQuery, and also one that uses simple vanilla JS in the click handler to determine which button was submitted.
Make it
input type=button"
and then do the work separately.Here is an example:
I give you an example for your reference: