سؤال

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() );
});
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

For something like focusout (or any other jQuery event) you can do:

$(".class-name").focusout( function() {
   //`this` refers to the object that was focusouted on
});

this always refers to the object the event happened on

Did you try it? It should definitely work, but without(!) the "this" parameter. "this" will automatically refer to the current element, no need to pass it to the function as a parameter. Just omit it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top