8

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!

5 Answers 5

9

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

This works well! I can remove the attachEvent stuff and already have jQuery so I might as well use it. Thanks!
I hope this helps you , Jsfiddle code is here : jsfiddle.net/pragneshkaria/eusyrdmy/2
2

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;
    }

}

Comments

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;
}

Comments

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);
};

1 Comment

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.
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"]' )

1 Comment

This allows you to target the individual buttons, but doesn't use logic based on which button submitted the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.