1
$(document).on('keyup submit click', '#myForm', function(e) {
    console.log(e.type);
});

Here e.type returns either click or keyup but not the status of submission. How can I check if the form was submitted?

bhansa
  • 7,282
  • 3
  • 30
  • 55
  • You can refer this [link](https://stackoverflow.com/a/7410096/5971348) for help – Zeeshan Afzal Satti Mar 20 '18 at 09:48
  • You need to attach the `submit` event handler to the form directly, not to the submit button. – Zenoo Mar 20 '18 at 09:49
  • The status itself is probably not resolved by the time the code is evaluated, have you tried adding a Promise to it?https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises – versvs Mar 20 '18 at 09:51

1 Answers1

0

The document you attached the handler to, doesn't have a submit event, hence it won't fire.

What you could do though, if not to attach the submit handler to the form, would be to check if the e.target is the actual submit button, e.g. check if it has the type="submit" attribute.

if (e.target.hasAttribute('type') &&
    e.target.getAttribute('type').toLowerCase() == 'submit') {...}

With that you could check if the submit button were clicked.

If the submission is called in any other way, you need to watch that.

Do note, if you have a validation function called, the form might not actually be submitted, so if you need to know it were, you need to attach the submit handler to the form.

Asons
  • 84,923
  • 12
  • 110
  • 165