Handle Keypress Event on a Search Box in jQuery

12:20 AM Unknown 0 Comments

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();
}
}
}

0 comments: