Prevent default and then allow again

Hey there,

I have an onclick handler which executes and jquery ajax load function but I need to validate a form first before executing the load function. I don’t know how to prevent the load from occurring until the validate is complete. I am validating using the jquery validate plugin. Any ideas?


The jQuery validate documentation page has a section called “Plugin Methods”. At the top of that list of methods is one called “validate( options )” that you can click on to get more details.

The “validate( options )” page has two tabbed sections, called Overview and Options.

In the Options tab is one called submitHandler which shows some code to achieve what looks to be precisely what you need.

Hey there,

Thanks for pointing me in the right direction, got it sorted :). I have been working on an opencart mod.

The fact that a bit cms system like opencart relies on ajax for its checkout funcion showed me that a lot of people aren’t considering users with js disabled nowadays and I have always learnt that I should take this into consideration.

I can see that you’re a very experienced js developer, what do you think?


My belief is that JavaScript should be used to enhance web pages. When scripting is used to supply behaviour that has no alternative means of occurring, that tends to lead to bad places.

So how a CMS should handle that, is to use standard basic form submissions to do the work, and then to use JavaScript to take over the form submission in order to provide an enhanced user experience, in this case that being to submit using ajax so that a page reload is not needed.

Thanks I really appreciate the advice.

BTW how do you debug your scripts? I have been using firebug and console.log() to dump variables but I would like to learn how to trace through properly, I have tried the Venkman debugger and can’t seem to get it to work. Do you know of any good tutorials on tracing though with either Venkman or firebug or anything similar?


Silversurfer :wink:

I debug them using the built-in debugger that comes with Google Chrome.

For basic info about using Firebug with JavaScript, you can start at:
JavaScript Debugger and Profiler : Firebug
Script Panel - FirebugWiki

A tutorial on using it is at:
Debug JavaScript with Firebug | Code Central

Typically I breakpoint at a certain place, then use the step commands to walk on through the execution to help narrow down the cause.

That one is just about identical in how it works to the Venkmann extension for Firefox (and also as similar to the ones built into Safari, IE8, and Opera).

About the only difference between them all is where you find the option to open them.

Thanks for the advice both,

My javascript is getting better than my ability to guess what outcome I will get from it so I must learn asap to debug properly!