Link to home
Start Free TrialLog in
Avatar of mike99c
mike99c

asked on

Blur event handler gets called after form submission handler but I want it called before

Here is a link to a simple test page to illustrate my issue:
http://www.dressorganic.co.uk/blurtest/

I have a form field in which when you enter a username it calls an AJAX post method to call a server side script to check the username is unique. This is all working fine.

Hence if you type the username "olduser" then click on the "Your name" field, you will see the blur event gets triggered and a message notifying that the username is already taken is displayed. The additional "UsernameUnique" is just a variable to show a flag has been set.

Now please click on http://www.dressorganic.co.uk/blurtest/ again and add "olduser" to the username but instead of clicking on "Your name" go straight to clicking the save button. You will notice from the alerts that the form submission gets handled first followed by the blur event handler. Unfortunately the blur event sets the "UsernameUnique" flag which the submit handler is supposed to look at. Hence the submit handler does not see UsernameUnique = false so the form gets submitted.

Can somebody please explain how I can make the blur handler get called first followed by the submit handler? Alternatively if there is a better way to handle this then please explain.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Your Blur function does actually get called first, but it's an asynchronous call (AJAX calls usually are), so your blur function gets called, and while it's off doing it's job, your code carries on into the Submit function...

You may have to change your AJAX call to be synchronous (kind of counter-intuitive but may be your only option)
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mike99c
mike99c

ASKER

Thanks Chris, I have applied it here:
http://www.dressorganic.co.uk/blurtest/index2.asp

It works perfectly. I will use the long hand method on future as it makes sense.
Hey Mike,

The shorthand methods are fine if you don't need any of the advanced config options (get, post, load, getJSON etc). It's when you need to override a default setting that you'll need to go long-hand!!

Pleased you got it working - be aware though, that making your AJAX calls synchronous will cause lag in your application as the code now won't continue running until the server has responded - this may only be a couple of milliseconds but it could be a several seconds.
Avatar of mike99c

ASKER

Ok thanks for this. I think synchoronous i what i need in this case anyway so it works out fine and in any case the server side scripting is not likely to be too intense.