else if blocks

else if blocks

I'm trying to create an autocompleter  and I want to create an else if statements.

here is my logic.  Bind a "keyup" event to the input (and receive the "event object" as e). In the event:

  • This event fires every time the user presses a key in the input. Capture the value of the input to a variable using .val()
  • If the length of the value is less than 2 characters:

    • Hide the results DIV (using a fadeOut animation)
    • Exit the event by using return false.
  • The key pressed is stored in e.which(Up = 38, Down = 40, Enter = 13, Esc = 27).
  • Create a conditional using ifand else ifblocks to test which key was pressed (It was either up, down, enter, escape. Any other key we assume is a letter).
  • End the conditional with an elseblock (if the key pressed was anything other than the above)...

 Can anyone help me get this right?  I would greatly appreciate it. I'm getting frustrated because I don't know what to do.

  1.    var searchText =  $("#searchtext").keyup(function(e) {
               console.log(test , 'keyup1')
                
              if ($(this).val().length < 2){
                 
                 
                   var test =  $("#searchresults").fadeOut("slow");
                   console.log(test , 'fadeOut')
              
                                    
    } else if ( e.which === 38 ) {
        searchText();
       
    } else if ( e.which === 40 ) {
        searchText();
       
    } else if ( e.which === 13 ) {
        searchText();
       
    } else if ( e.which === 27 ) {
        searchText();
       
    } else {
       
    }
                  
              return false
             

                });