Handle Keypress Event on a Search Box in jQuery

I have this problem before when I tried to search a keyword from a database using jQuery-AJAX-PHP. Where in the query is fired up multiple times which slows down process. After searching reading various solution for this problem. I've came up with the jQuery function below.

This handles the keypress event of the textbox with an id keyword.
$('#keyword').live('keypress', KeywordSearch);


Javascript Function:
function KeywordSearch(event) {

 if (event.handled !== true)
 {   
  if (event.keyCode == 13){    
   var type = $('#searchType').val();
   var keyword = $('#keyword').val();
   
   var result = $.ajax({
    type: "GET",
    url: "search.php?keyword=" + keyword + "&type=" + type,
    async: false}).responseText;
   
   $('#results').html(result);
   
   $('#myTable').fadeIn('fast');
   event.handled = true;
   event.preventDefault();
  }
 }   
}

Comments

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Make Windows Form Transparent using Visual Basic 6/VB6

How to Set Windows Form Always on Top of Other Applications in VB6