views:

339

answers:

2

I have a form with many fields and 2 submit buttons. I want to run a different check depending on which submit button i click on. I tried writing the code below but it is wrong (i dont know JS) and nothing happens (bad syntax?). How do i figure out which of the two buttons was clicked?

document.getElementById('sub1').onclick = checkForm;
document.getElementById('sub2').onclick = checkForm;

function checkForm(e)
{
    var e = e || window.event;
    var o = e.srcElement || e.originalTarget;

    if(o.id=="sub1")
        return checkNotNull();
    else
        return checkSamePass();
}
+1  A: 

Why make it so complicated?

function checkForm(button) {

  formOk = false;

  if (button = 'first') {
    //check form
    if (formOk) myform.submit();
  }

  if (button = 'second') {
    //check form using other logic
    if (formOk) myform.submit();
  }

}


<input type="button" onClick="checkForm('first');" value = "button 1">
<input type="button" onClick="checkForm('second');" value = "button 2">
code_burgar
+1  A: 

I put your code into a test page, and it works fine on both FF and IE. The problem likely lies with the two functions you are calling, checkNotNull() and checkSamePass(). I would check that they are working and are properly returning.

You can verify that the o.id is correct by the time it gets to the if/else statement by putting in an alert(o.id).

zombat
Another possible point of failure: make sure you put this script after your form, or inside an onload/ready function.
Emmett
oops, i had a typo in the id name :x. Thanks for telling me it does indeed work.
acidzombie24