Can I submit form with multiple submit buttons using jQuery?

Yes, you can submit forms with multiple submit buttons using jQuery. Attach a custom click handler to all the buttons and then check which button is clicked.

When working with multiple submit buttons, you need to prevent the default form submission behavior and implement custom logic to determine which button was pressed. This allows you to perform different actions based on the specific button clicked.

Example

The following example demonstrates how to handle multiple submit buttons using jQuery −

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <script>
      $(document).ready(function(){
         $("#myform button").click(function (ev) {
            ev.preventDefault();
            
            if ($(this).attr("value") == "button1") {
               alert("First Button is pressed - Form will submit");
               $("#myform").submit();
            }
            
            if ($(this).attr("value") == "button2") {
               alert("Second button is pressed - Form did not submit");
            }
         });
      });
   </script>
</head>
<body>
   <form id="myform">
      <input type="email" name="email" placeholder="Enter email" required /><br><br>
      <button type="submit" value="button1">Submit Form</button>
      <button type="submit" value="button2">Cancel</button>
   </form>
</body>
</html>

How It Works

In this example −

  • The $("#myform button") selector targets all buttons within the form
  • ev.preventDefault() stops the default form submission behavior
  • We use $(this).attr("value") to check which button was clicked
  • Based on the button's value, different actions are performed
  • Only the first button actually submits the form using $("#myform").submit()

This approach gives you complete control over form submission behavior when using multiple submit buttons with jQuery.

Updated on: 2026-03-13T20:52:12+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements