Jump to content

prevent.default


jimfog

Recommended Posts

I am about to ajaxify a form and my question is where in the context of a form prevent.default is used exaxtly. I know what(in general) prevent.default does but where exactly does it help with form submission.

Link to comment
Share on other sites

ok...it prevents submitting the form.But can you give me an example where this might be useful.

Link to comment
Share on other sites

You would prevent the form from submitting normally and submit it via ajax. If Javascript is disabled then that code never runs and the form submits normally.
This is what I had in my mind when I was asking the question.So...in ajax forms we always use prevent.default-for the reasons you mention.
Link to comment
Share on other sites

You will probably need to send data to the server script so it knows whether to respond with a complete document or with data formatted for AJAX. Testing for the value of a hidden form field is usually enough.

Link to comment
Share on other sites

You will probably need to send data to the server script so it knows whether to respond with a complete document or with data formatted for AJAX. Testing for the value of a hidden form field is usually enough.
Ι did not quite understood that.From the moment you send something with AJAX I think it is natural to expect the response in AJAX. Clarify more if you want... Αnd most importantly does form data go with json to the server-this is something I really do not know... Edited by jimfog
Link to comment
Share on other sites

I was referring to this:

If Javascript is disabled then that code never runs and the form submits normally.

Depending on your application, you may want to plan for this case.

Link to comment
Share on other sites

I was referring to this:
If Javascript is disabled then that code never runs and the form submits normally.

Depending on your application, you may want to plan for this case.

probably I will not give the option of a fallback to js---it will work only with js
Link to comment
Share on other sites

probably I will not give the option of a fallback to js---it will work only with js
Then rather than type="submit" you can simply use type="button." I have not used prevent.default with form submission because it seems more straightforward to simply change the type of button when JS initializes. You will probably be changing the hidden field in the form anyway. In many other situations there is no way to avoid the need for prevent.default. For example, what if you don't want a mouse-drag to be allowed to select anything on a page? Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...