When using the focusout function, I encountered an issue where clicking on a title in the search results would prevent redirecting to the title page. Although the function worked properly when closing the search results by clicking on a different element on the page, the results also closed when clicked on.
I attempted using the blur function as well as hide() and show(), but the outcome remained the same. Even though I have set .result display to none, it should not hinder me from selecting a search result. What could be causing this issue?
// jquery.js
$(document).ready(function() {
$('.search-box input[type="text"]').on("keyup input", function() {
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if (inputVal.length) {
$.get("includes/searchbar.inc.php", {
term: inputVal
}).done(function(data) {
// Display the returned data in browser
resultDropdown.html(data);
$('.result').css('display', 'block');
$("#searchboxy").focusout(function() {
$('.result').css('display', 'none');
});
$("#searchboxy").focusin(function() {
$('.result').css('display', 'block');
});
});
} else {
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result", function() {
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- header.php -->
<div class="search-box" id="searchboxy">
<input type="text" autocomplete="off" placeholder="Search for a game or genre..." />
<div class="result" id="resultsearch"></div>
</div>