Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring ajax search onkeyup with jquery

My Script to call ajax

<script language="javascript">
function search_func(value)
{
    $.ajax({
       type: "GET",
       url: "sample.php",
       data: {'search_keyword' : value},
       dataType: "text",
       success: function(msg){
                   //Receiving the result of search here
       }
    });
}
</script>

HTML

   <input type="text" name="sample_search" id="sample_search" onkeyup="search_func(this.value);">

Question: while onkeyup I am using ajax to fetch the result. Once ajax result delay increases problem occurs for me.

For Example While typing t keyword I receive ajax result and while typing te I receive ajax result when ajax time delay between two keyup sometime makes a serious issue.

When I type te fastly. ajax search for t keyword come late, when compare to te. I don't know how to handle this type of cases.

Result While typing te keyword fastly due to ajax delays. result for t keyword comes.

I believe I had explained up to reader knowledge.

like image 716
Mohan Ram Avatar asked Apr 12 '11 14:04

Mohan Ram


People also ask

How do you call Ajax from Keyup?

$(function(){ // Document is ready $("#sample_search"). keyup(function() { $. ajax({ type: "GET", url: "sample. php", data: {'search_keyword' : value}, dataType: "text", success: function(msg) { //Receiving the result of search here } }); }); });

What is Keyup function in jQuery?

The keyup() is an inbuilt method in jQuery which is used to trigger the keyup event whenever User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Syntax: $(selector).keyup(function) Here selector is the selected element.

Why is Keyup not working?

keyup / keydown seem to only work on elements that are present at document. ready . If you are triggering your event from elements that were altered or injected post pageload, these events will not fire.

What does Onkeyup do in JavaScript?

Definition and Usage The onkeyup attribute fires when the user releases a key (on the keyboard). Tip: The order of events related to the onkeyup event: onkeydown. onkeypress.


1 Answers

You should check if the value has changed over time:

var searchRequest = null;

$(function () {
    var minlength = 3;

    $("#sample_search").keyup(function () {
        var that = this,
        value = $(this).val();

        if (value.length >= minlength ) {
            if (searchRequest != null) 
                searchRequest.abort();
            searchRequest = $.ajax({
                type: "GET",
                url: "sample.php",
                data: {
                    'search_keyword' : value
                },
                dataType: "text",
                success: function(msg){
                    //we need to check if the value is the same
                    if (value==$(that).val()) {
                    //Receiving the result of search here
                    }
                }
            });
        }
    });
});

EDIT:

The searchRequest variable was added to prevent multiple unnecessary requests to the server.

like image 76
Void Avatar answered Oct 14 '22 13:10

Void