$(document).ready(function() {
$('#input').focusin(function() {
if ($(this).val() != '') {
$('#div').show();
} else {
$('#div').hide();
}
$('#input').keyup(function() {
// If not empty value, show div
if ($(this).val() != '') {
$('#div').show();
} else {
$('#div').hide();
}
});
});
$('#input').focusout(function() {
$('#div').hide();
});
});
#div {
display: none;
position: absolute;
bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div'>
<a href='website.com'>Link From AJAX<a>
<a href='website.com'>Link From AJAX<a>
<a href='website.com'>Link From AJAX<a>
</div>
<input id='input' type='text'>
In this code snippet using jQuery, the #div
element is set to display when text is entered inside the #input
. However, clicking on the #div
causes it to disappear and prevents the link from working properly.
To resolve this issue and keep the #div
displayed when either the #div
or #input
elements are clicked, an additional line of code within the .focusin function
can be added. Additionally, removing the position
and bottom
CSS properties will help fix the previous error.
The problem with the disappearing #div
is related to the position: absolute; bottom 20px
line in the CSS styling.
After updating the code with the necessary adjustments, the functionality worked as intended, ensuring that the #div
remains visible when focused on the correct elements while hiding it when outside elements are interacted with.