7

I have a form with multiple submit buttons, and I'd like to capture when any of them are pressed, and perform different JS code for each one.

<form id="my-form">
    <input type="email" name="email" placeholder="(Your email)" />
    <button type="submit" value="button-one">Go - One</button>
    <button type="submit" value="button-two">Go - Two</button>
    <button type="submit" value="button-three">Go - Three</button>
</form>

Looking at an older answer, I can process all of the submit buttons in JS:

function processForm(e) {
    if (e.preventDefault) e.preventDefault();

    /* do what you want with the form */

    // You must return false to prevent the default form behavior
    return false;
}

var form = document.getElementById('my-form');
if (form.attachEvent) {
    form.attachEvent("submit", processForm);
} else {
    form.addEventListener("submit", processForm);
}

But how can I discriminate amongst the different submit buttons? Is there a way to get the value and perform logic from there?

I don't need to have three submit buttons, per se... I just need three different buttons in a form to perform three different actions.

Thanks!

Community
  • 1
  • 1
Berto
  • 702
  • 2
  • 9
  • 19

5 Answers5

8

you can attach a custom click handler to all buttons, and that way you can check which button is clicked before submitting the form:

Live Example

$("#my-form button").click(function(ev){
    ev.preventDefault()// cancel form submission
    if($(this).attr("value")=="button-one"){
        //do button 1 thing
    }
    // $("#my-form").submit(); if you want to submit the form
});
Banana
  • 7,424
  • 3
  • 22
  • 43
1

use this

function processForm(e) {
if (e.preventDefault) e.preventDefault();
/* do what you want with the form */
var submit_type = document.getElementById('my-form').getAttribute("value");
if(submit_type=="button-one"){

}//and so on
// You must return false to prevent the default form behavior
return false;
}
aM-Vee
  • 403
  • 2
  • 10
1

HTML:

<form id = "form" onSubmit = {handleSubmit}>
<input type="email" name="email" placeholder="(Your email)" />
<button type="submit" value="button-one">Go - One</button>
<button type="submit" value="button-two">Go - Two</button>
<button type="submit" value="button-three">Go - Three</button>
</form>

JS:

const handleSubmit = e => {
    e.preventDefault();

    var ans = document.activeElement['value'];
    console.log(ans);
};
sauravjoshi23
  • 837
  • 11
  • 9
  • 1
    Didn't know about activeElement before. This helped me as I had multiple dynamic forms as well as all input fields in the form were dynamic. Moreover, I had two submit-type buttons. I was using jquery's serializedArray() to get all the values of the form using $("form").submit and reading using $(this). activeElement helped me to get the type of submit button clicked. Thanks. – curious.netter Dec 08 '21 at 14:58
1

You can also use the read-only submitter property of a SubmitEvent.

HTML (same as you have):

<form id="my-form">
    <input type="email" name="email" placeholder="(Your email)" />
    <button type="submit" value="button-one">Go - One</button>
    <button type="submit" value="button-two">Go - Two</button>
    <button type="submit" value="button-three">Go - Three</button>
</form>

JS:

let form = document.getElementById("my-form");
form.addEventListener("submit", handleSubmit);

function handleSubmit(event) {
    event.preventDefault()
    alert(event.submitter.value) // shows a different value depending on button pressed

From there on, it's easy to use submitter as a control flow, e.g.:

let form = document.getElementById("my-form");
form.addEventListener("submit", handleSubmit);

function handleSubmit(event) {
    event.preventDefault()
    submitter = event.submitter.value
    switch (submitter) {
        case "button-one":
            alert(1)
            break;
        case "button-two":
            alert(2)
            break;
        case "button-three":
            alert(3)
            break;
    }

}
swimmer
  • 1,971
  • 2
  • 17
  • 28
0

But how can I discriminate amongst the different submit buttons? Is there a way to get the value and perform logic from there?

Yes, you can use querySelector to grab elements with attributes.

document.querySelector('#my-form button[value="button-one"]' )
Matt Styles
  • 2,442
  • 1
  • 18
  • 23
  • 3
    This allows you to target the individual buttons, but doesn't use logic based on which button submitted the form. – ricdesi Nov 03 '16 at 15:17