'Which jQuery event can I use to avoid input event trigger when click outside the input
I have an input that is searching with AJAX call through the database and display results. Currently, when I focus the input and start typing the results are shown after I finish typing. But right when the results are shown and I click outside the input (when I remove the focus from the input) the input event is triggered again. How can I avoid that?
<input name="searchWord" id="searchW0rd" type="text" placeholder="Search for a company..." autocomplete="off">
$("#searchW0rd").on('input', function(e) {
clearTimeout(timeoutID);
const value = e.target.value;
timeoutID = setTimeout(() => showResult(1), 500);
});
I tried with
$("#searchW0rd").on('input', function(e) {
if($(e.target).is("#searchW0rd")) {
clearTimeout(timeoutID);
const value = e.target.value;
timeoutID = setTimeout(() => showResult(1), 500);
}
});
but it didn't worked. Any ideas? Thanks a lot!
I also have this AJAX setup (if matters)
$(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).ajaxStart(function () {
$(".companies").empty();
$("#companies-pages").empty();
$("#placeholder-companies").removeClass('d-none');
});
$(document).ajaxSuccess(function () {
$("#placeholder-companies").addClass('d-none');
});
$(document).ajaxStop(function () {
$("#placeholder-companies").addClass('d-none');
});
showResult(1);
});
and the showResult() function is actually the AJAX call that is displaying the results in a div.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
