I am attaching listeners to divs that worked great. There are portions in my App that some of these divs gets hidden using hide() function of JQUERY which pretty much uses the css:
style="display: none;"
When my App brings them back using show() function, the listeners seem to stop working, like they’ve been detached due to me hiding the divs.
For additional info, the listener I am trying to attach is actually the JQUERY AUTOMCOMPLETE plugin
https://api.jqueryui.com/autocomplete/
I basically call this function to attach this to a target element
function my_autoComplete(elem,url,params_r=[]){
let cache = {};
let minLength = params_r['minLength'] || 2;
let zindex = params_r['zindex'] || "9999";
let bgcolor = params_r['bgcolor'] || "#0a53be";
$(elem).autocomplete({
minLength: minLength,
select: function( event, ui ) {},
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
$.getJSON( url, request, function( data, status, xhr ) {
cache[ term ] = data.data;
response( data.data );
});
}
});
}
and i call it as:
my_autoComplete("#search_field","http://myapi.com");
This works perfectly fine and the autocomplete functions attaches to the target element but the moment i hide and show div where the element is in, the automcomplete functions doesn’t work.
My current work around now is to show the div block and then call the my_autoComplete function on the element, again. I have to keep doing this everytime i re-show the div block and it seems to have solved my problem, for now.
Is there a way to prevent listeners from being detached or stop working when you the elements they’re attached to are hidden temporarily?
2
Answers
there might be an error somewhere else.
As you can see here:
As you can see, the triggers are not disappearing at both.
You probably have something else going on that prevents/kills the listener. The default autocomplete setup doesn’t do that on show/hide: