Take the 2-minute tour ×

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) {
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
It say's unexpected "return" and unexpected ")" –  Netra Sep 30 '12 at 21:00
I am sorry, I fixed it. It was more of psueodocode. –  ge0man Sep 30 '12 at 21:07
now I get the same errors like in the first comment, unexpected "return" and unexpected ")". –  Netra Sep 30 '12 at 21:11

Your Answer


