KCL
KCL

Reputation: 6883

Ignoring onSubmit when the submit button is clicked

I have a form where I've specified onSubmit="validate()", but I want to ignore the validation if the submit-button was clicked. Is there a good cross-browser way of detecting if the submit button was clicked and thus ignoring the validation?

Upvotes: 2

Views: 6199

Answers (5)

Quentin
Quentin

Reputation: 944439

The submit event only fires if the form is submitted by the user; not if it is submitted via JS.

Therefore:

<input type="submit" onclick="this.form.submit(); return false;">

If JS is not available, this acts like a normal submit button … and the onsubmit still fails to fire as it also requires JS.

(Attaching events using JS instead of intrinsic event attributes is, as usual, preferred by excluded from this example for the sake of clarity)

Upvotes: 2

Ruan Mendes
Ruan Mendes

Reputation: 92324

Just return false, or preventDefault from your submit button handler

Upvotes: -1

oezi
oezi

Reputation: 51817

you can try to use a <input type="button"... with an onClick that submits the form - a javascript .submit() doesn't fire the onSubmit-function of the form.

Upvotes: 1

geocar
geocar

Reputation: 9305

Did you try this?

<input type="submit" onclick="void(window.validate=function(){return true;})" value="Submit" />

Upvotes: 0

Marcos Placona
Marcos Placona

Reputation: 21730

Why don't you use a button instead of a submit, and set it's action on the click of the button? That way you can control if you want to validate, submit, or whatever else you like.

Upvotes: 3

Related Questions