So I'm trying to prevent form submit if the input is empty. The code looks something like this:
$(document).on("submit", "form#input_form", function(e){
if(!$(this).find("input[type=text]").val()){
e.preventDefault();
e.stopPropagation();
}
});
This doesn't work. The request just goes through even though it executes both preventDefault and stopPropagation.
However, if I do the following it successfully prevents submit:
$("form#input_form").on("submit", function(e){
if(!$(this).find("input[type=text]").val()){
e.preventDefault();
e.stopPropagation();
}
});
I don't understand what the difference is, it's basically the same thing except that the former attaches the handler at document level whereas the latter does it to the form itself. By the way, I'm using form_for
helper to render the form.