Analog
Analog

Reputation: 261

get submit button name or value in a multi form

I know there are a ton of posts on this but I didn't have any luck in finding something that worked.

I have multiple forms on the page that I want to be able to submit one by one using submit buttons within each form. This is currently working for me:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    <!--
    $(document).ready(function() {
        $(".do_submit").click(function() {
            var element = $(this);
            var Id = element.attr("id");
            var val = $("#val" + Id).val();
            var anotherval = $("#anotherval" + Id).val();
            alert("Id " + Id + " val " + val + " anotherval " + anotheval);
                    // How do I get the name of the value of whatever submit button was clicked
        });
    }); 
    -->
</script>   
</head>
<body>
<form action="" method="post" name="form1" />
  <input type="hidden" name="val" id="val1" value="a" />   
  <input type="hidden" name="anotherval" id="anotherval1" value="b" />  
  <input type="submit" name="SavePlantInfo" class="do_submit" id="1" value="Save" />
  <input type="submit" name="SavePlantInfoAndEmail" class="do_submit" id="1" value="Save And Email" />                                   
</form>

<form action="" method="post" name="form2" />
  <input type="hidden" name="val" id="val2" value="a" />   
  <input type="hidden" name="anotherval" id="anotherval2" value="b" />  
  <input type="submit" name="Save" class="do_submit" id="2" value="Save" />
  <input type="submit" name="SaveAndEmail" class="do_submit" id="2" value="Save And Email" />                                   
</form>
</body>
</html>

What I can't figure out is how to know which of those 2 submit buttons were clicked on. I want to be able to either get the name (preferred) or the value of whichever submit button was clicked.

TIA!

Upvotes: 0

Views: 2695

Answers (3)

thecodeparadox
thecodeparadox

Reputation: 87073

$(".do_submit").click(function() {
    var buttonName = this.name; // OR $(this).attr('name')
    // your code
});

Upvotes: 0

FLF
FLF

Reputation: 157

try like this;

 $(".do_submit").click(function() {
          var ele=$(this).val();
    if(ele=='Save')
    {
    /// your code
    }
    else
    {
    // save and email
    }
    });​

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

Did you try:

$(this).attr("name"); ?

Upvotes: 3

Related Questions