hunter hunter - 2 years ago 71
Javascript Question

How can I get the button that caused the submit from the form submit event?

I'm trying to find the value of the submit button that triggered the form to submit

$("form").submit(function() {


I could possibly fire a $("input[type=submit]").click() event for each button and set some variable, but that seems less elegant than some how pulling the button off of the the form on submit.

Answer Source

I implemented this and I suppose it will do.

$(document).ready(function() {
    $("form").submit(function() { 

    var val = $("input[type=submit][clicked=true]").val()

    // DO WORK


and this is the submit button event that sets it up

$("form input[type=submit]").click(function() {
    $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
    $(this).attr("clicked", "true");

Thanks for the responses, but this isn't terribly inelegant...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download