Whenever the input text value is not empty, I want my .removetext
div to be displayed, but it's not working correctly. When I type something after the second character, my .removetext
gets hidden, but it shouldn't be hidden when the input is not empty.
Where did I go wrong? I think I am misunderstanding the keydown event.
$(document).ready(function() {
$('.search-hotels').on('input', function() {
var val = $.trim(this.value);
if (!val == "") {
$(".removetext").show(val.length > 0, function() {
var clear = $(this);
clear.on('click', function() {
alert('hey');
})
});
} else {
$(".removetext").hide();
}
});
});
.main {
position: relative;
width: 40%;
}
.search-hotels {
width: 100%;
padding: 15px;
border: 3px solid #ccc;
}
.removetext {
position: absolute;
right: 0;
top: 25%;
font-size: 23px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<input type="text" class="search-hotels">
<div class="removetext">×</div>
</div>