Reputation: 27713
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() );
});
Upvotes: 0
Views: 10109
Reputation: 140230
Assuming you realize the magical nature of this is what you want:focusout
over blur
,
$("#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
.
Upvotes: 9
Reputation: 146310
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
Upvotes: 5
Reputation: 16544
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.
Upvotes: 2