I’m using this js to get results from a php file and output them, this happens on keyup. Everything is working well but the problems is when I type something and than erase the last result won’t erase. How can I make the script erase the result if the input is empty or if there is only one character typed?
$('#username-search').keyup(function() {
if (this.value.length < 2) return;
var usersearch = $(this).val();
$.post("autocomplete_user.php", {
results: usersearch,
},
function(data) {
$('#data').html(data)
});
});
Update your if statement to the following:
if (this.value.length < 2) {($(‘#data’).html() != “” ? $(‘#data’).html(“”) : return true)}
What this does is, if the input goes below 2, it will check to see if that DOM element with the id of DATA has anything in it, if it does, it erases the data, if not then it just returns.