jqueryjquery-focusout

Jquery focusout function


Is possible to pass the element to the focusout function in Jquery? I would like to tie all textboxes focusout event in one statement. So my solution would be to make them all the same class and then do something like

$(".class-name").focusout( function() {
//do whatever
});

But I would like to get the value of the element inside the focusout without having to refer to it by its id, so is something like this possible?

$(".class-name").focusout( function(this) {
alert( $(this).val() );
});

Solution

  • Assuming you realize the magical nature of focusout over blur, this is what you want:

    $("#the-common-parent-of-all-inputs").focusout( function(e) {
    alert( e.target.value );
    });
    

    You only need to bind one focusout to the common parent of the input elements.This is the only reason to use focusout.

    You may not realize it but when you do $(".class-name").focusout it will just bind to each element individually, which defeats the whole purpose of focusout.