I'm using focusout
in a code and I need to know which element was clicked to fire the focusout
because I need to execute a specific function if a specific element was responsible in firing focusout
I tried to use stopPropagation
but it dosen't work
$('#buscar_menu .select2-search__field').focusout(function(e){
e.stopPropagation();
/*if(e.target.classList[0] == "search_field"){
console.log("eureca");
}else{
console.log("not eureca");
}*/
});
Using jQuery
you can add the data attribute with a custom property name like data-responsible-element
; this can be retrieved using jQuery's
data()
method to retrieve the element that lost focus.
In short: Set data-{custom-property}="{some-value}"
in an element and after the event has been triggered retrieve {some-value}
from data-{custom-property}
using data()
method.
With this method you can set {some-value}
to be an unique identifier
for that element which would enable you to query
the element.
$('.focusable').focusout(function(e){ e.stopPropagation(); var element_that_lost_focus = $(this).data('responsible-element'); console.log(element_that_lost_focus + ' just lost focus!? D:'); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="focusable" placeholder="Juan" data-responsible-element="Juan" /> <input class="focusable" placeholder="Carlos" data-responsible-element="Carlos" /> <input class="focusable" placeholder="Martín" data-responsible-element="Martín" /> <input class="focusable" placeholder="John Doe" data-responsible-element="John Doe" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.