views:

642

answers:

1

I am trying to use the focusout(http://api.jquery.com/focusout/) function in jQuery, to perform a function after the user focus away from an input form field, but it's not working.

Here is my code:

$("#employee_id").focusout(function(){
  var employeeId = $("#employee_id").val();
  if(employeeId == '') {
    $("#employee_id").after('<div class="error">Your employee id number is required.</div>');
    hasError = true;
  }
});

I am using jquery-1.3.2.min.js since another plugin I am using(qtip) is giving me an error when I try to use jquery-1.4.2.min.js.

How can I make the focusout event work, or is there another way to do what I am trying to accomplish?

+3  A: 

Since you just seem to care about that field and not the parent/bubbling, use .blur():

$("#employee_id").blur(function(){
  if($(this).val() == '') {
    $(this).after('<div class="error">Your employee id number is required.</div>');
    hasError = true;
  }
});
Nick Craver
Very nice! Thank you! What is parent/bubbling? What does that mean?
zeckdude
@Chris - `focusout` bubbles up, for example if you're in a `<input>` that's in a `<div>` and you move to another one in a different `<div>`, `focusout` triggers for the `<input>`, then the `<div>` it was in because it bubbles through the parent.An easier example is links, click a link, you get a click for the link, then whatever the link's in, and whatever that's in, it goes all the way through the parents that contain the link, we refer to that as `bubbling`. `blur()` and `focus()` **don't** do this, they're the few things that don't.
Nick Craver