0

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

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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

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">

Comments

Your Answer

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