0

i have a html form with two submit buttons and two textboxes

here's the form

<form action="domains.php#searchdomain" method="post" name="m_domain"
      onsubmit = "return primalValidate()">
 <table class="dataTable" width="100%" border="0" cellspacing="0" cellpadding="5" id="" style="text-align:center; margin-top:0px; border-left:1px solid #ddd; border-right:1px solid #ddd; border-top:1px solid #ddd;">
          <tr>
            <td align="left" colspan="2"><div id="display_message" <?php echo $sstyle; ?>><?php echo $dis_msg; ?></div></td>
          </tr>
          <tr>
            <td align="left">Search Domain</td>
            <td align="left" style="display:none;" id="apply_text">Replace Selected Domains With</td>
          </tr>

          <tr>
            <td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td><input class="input_field" name="search_domain" id="search_domain" value="<?php echo $search_domain; ?>" type="text"></td>
                  <td>&nbsp;</td>
                  <td><input type="submit" class="btn blue_button" 
                    name="submit_domain_form" id="submit_search_button"
                  value="Search" /></td>
                </tr>
              </table></td>
            <td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0" id="apply_button" style="display:none;">
                <tr>
                  <td><input class="input_field" name="domain_name_url" id="domain_name_url" value="<?php echo $domain_name_url; ?>" type="text"></td>
                  <td>&nbsp;
                  <input name="domain_replace_id" id="domain_replace_id" 
                  value="" type="hidden">
                  <input name="domain_replace_link" id="domain_replace_link" 
                  value="" type="hidden">
                  </td>
                  <td><input type="submit" class="btn blue_button" 
                    name="submit_domain_form" id="submit_replace_button"
                  value="Apply" /></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td align="center">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><input type="checkbox" name="status_domain" id="status_domain" <?php if($status_domain){?> checked <?php } ?>>&nbsp;Include inactive campaigns in search.</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                    </tr>
                </table>
            </td>
            <td align="center">&nbsp;</td>
          </tr>

        </table>     
</form>

now I am trying to make a common validation form like this...

function primalValidate()
{
    var action = document.forms["m_domain"]["submit_domain_form"].value;
    if(action=="Search")
    {
        //validation condition 1;
        //return false;
    }
    else if(action=="Apply")
    {
        //validation condition 2;
        //return false;
    }

}

but the problem is none of the validation condition is working....

when I am trying to alert the value of action, i.e.

alert(action); i am getting blank alert

These are the two submit buttons in the form

<input type="submit" class="btn blue_button" 
                        name="submit_domain_form" id="submit_search_button"
                      value="Search" />

<input type="submit" class="btn blue_button" 
                        name="submit_domain_form" id="submit_replace_button"
                      value="Apply" />
Saswat
  • 12,320
  • 16
  • 77
  • 156

1 Answers1

2

Try this,

var action = $("input[type=submit]:focus").val();;

Demo

And if you want it by submit button name then try,

var action = $("input[name=submit_domain_form]:focus").val();

Try it here

Also, if you use enter key to submit then both the above will alert undefined. For this, you need some logic according to your HTML design like,

if(action==undefined){
    action=$('*:focus').closest('table').find(':submit').val();
    // you can use name=submit_domain_form instead of :submit as used in second updated answer
}

See working demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • i need to fetch it by name not id – Saswat Jan 19 '15 at 06:12
  • but one more thing, if someone doesn't click the submit button, but press enter when the focus is on a textbox, will this code work?? – Saswat Jan 19 '15 at 06:14
  • hello, are u there bro? – Saswat Jan 19 '15 at 06:18
  • @Saswat No, it wil not work on using enter key. Try my third updated answer. – Rohan Kumar Jan 19 '15 at 06:22
  • [See this answer](http://stackoverflow.com/questions/5721724/jquery-how-to-get-which-button-was-clicked-upon-form-submission). On submit of form you can get value of the button which submitted the form. – Airan Jan 19 '15 at 06:33
  • @RohanKumar,,,, can you be free for some time?, let me test and if i get some glitch then i can ask you for help... – Saswat Jan 19 '15 at 06:34