Reputation: 586
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() ?
Upvotes: 0
Views: 43
Reputation: 781096
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.
Upvotes: 2