Solved

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

Posted on 2013-11-26
5
330 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:mike99c
  • 3
  • 2
5 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39677309
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)
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39677332
To turn you request into a synchronous request, you'll need to use the full version of the AJAX call, not just the shorthand post()

$.ajax({
  type: 'POST',
  url: "check-username.asp",
  data: { TheUsername: Username },
  success: function(result) {
    if(result !== "unique"){  
      $('#Username').focus();
      UsernameUnique = false;
      alert("Username is already taken, UsernameUnique: " + UsernameUnique)
    } else {
      UsernameUnique = true;
    }
  },
  async:false
});

Open in new window

0
 

Author Comment

by:mike99c
ID: 39677360
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.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39677372
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.
0
 

Author Comment

by:mike99c
ID: 39677380
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now