rpivovar
rpivovar

Reputation: 3438

unable to call submit() after calling preventDefault()

I'm trying to validate a form after clicking the submit button, so I'm preventing the form from automatically posting and refreshing the page with preventDefault(). After calling preventDefault(), I call another function to validate, like so:

$("#submit").click(function(e) {

    e.preventDefault();

    validate();

});

Here, #submit is the ID for the submit input element.

At the end of validate(), I try to call submit() on the form element #homeForm, but it doesn't seem to fire.

validate() {

....

console.log('test'); // this is firing

$('#homeForm').submit();
}

I used a few other Stack Overflow answers including this one to arrive at this solution, but I'm not really sure why it's not working. What could be preventing the submit() function from firing?

Edit: I've added a bit of what this form looks like in the HTML

<form id="homeForm">
....

    <input id="submit" class="submit" type="submit" name="submit" value="Submit" action="javascript:void(0);">
</form>

Upvotes: 0

Views: 76

Answers (2)

kemika
kemika

Reputation: 124

The form action attribute should belong to the <form> element and should (probably) refer to a url

Upvotes: 2

Desmond
Desmond

Reputation: 149

The simpler method is to have the submit button call validate() function, then at validate function, if all is validated fine, then submit the form using $('#homeForm').submit(). This way you will never use preventDefault which I feel is the cause for whatever the reason.

Edit: Of course, then your button type should not be 'submit' for this.

Upvotes: 0

Related Questions