0

Ok, this feels like a very trivial question, and it might be. But I was hoping for someone to explain the difference, or point me towards a source which does explain, how to prevent a form from being submitted when validation checks find errors.

If I have an input of type submit, isn't it the click handler that would initiate form submission? Shouldn't it theoretically work if an e.preventDefault() is put inside the click event of the input described? And if it doesn't work, why? Should the preventDefault be implemented within the form submit event instead? (as from what I read, it is bad practice to prevent form submission in a click event- but I don't understand why!)

Ive read similar questions such as this ' Simplest way to disable button on submission of a form? ' , and it doesn't really answer my question. So, Anyone feels like helping a fellow out?

--Edit--

My actual question is : Is it bad practice to prevent form submission by adding an e.preventDefault in the click handler of an input type=submit. Or is it equivalent to adding the e.preventDefault in the jquery form.submit() ?

Community
  • 1
  • 1
LogixMaster
  • 586
  • 2
  • 11
  • 36
  • It's hard to answer your question posed this way. If you're having trouble preventing submission, show your code and someone should be able to point out the mistake you made. Calling `preventDefault()` should work, assuming you're binding the handler correctly. – Barmar Jan 09 '14 at 11:02
  • @Barmar, I ve editted my question! – LogixMaster Jan 09 '14 at 11:07

1 Answers1

2

It's probably best to put all your code in the form's submit() handler. A form can be submitted in a number of ways -- the user can click on the submit button, or they can press Enter in the last field of the form. These will all trigger the form's submit event, but not necessarily the click event of the submit button.

Barmar
  • 741,623
  • 53
  • 500
  • 612