Search code examples
jqueryautocompletelive

how to use .live with autocomplete in jquery?


I want to use autocomplete with .live function, but it gives syntax error

$("input[name=point]").live(function() {
    $(this).autocomplete({
        minLength:0,   //for local data
        delay:0,      //for local data
        source:function(request,response){
            //var param= {"action":"getSalePoints"};
            $.getJSON("controllers/Order.controller.php?action=getSalePoints",request,function(result){
                //create array for response objects
                var suggestions = [];
                //process response
                $.each(result, function(i, val){                                
                    //alert(val.number);
                    suggestions.push(val.number);
                });

                //pass array to callback
                response(suggestions);
            });


        },
        select: function( event, ui ) {
            var param={
                "action":"getSalePointNo",
                "point":ui.item.value
                };
            $.getJSON("controllers/Order.controller.php",param,function(result){            
                if(result == "0"){
                    $('#resultMsg').attr('class','errorMsg');
                }
                else{
                    alert(result);
                    $('[name=pointNo]', $(this).parents(".bill")).val(no);
                }

            });
        }
    });
});

Solution

  • As 'mu is to short' suggested, you are not supplying the event type to the live function, you can try using 'focus' as the event type, try:

    $("input[name=point]").live("focus", function() {
    $(this).autocomplete({
        minLength:0,   //for local data
        delay:0,      //for local data
        source:function(request,response){
            //var param= {"action":"getSalePoints"};
            $.getJSON("controllers/Order.controller.php?action=getSalePoints",request,function(result){
                //create array for response objects
                var suggestions = [];
                //process response
                $.each(result, function(i, val){                                
                    //alert(val.number);
                    suggestions.push(val.number);
                });
    
                //pass array to callback
                response(suggestions);
            });
    
    
        },
        select: function( event, ui ) {
            var param={
                "action":"getSalePointNo",
                "point":ui.item.value
                };
            $.getJSON("controllers/Order.controller.php",param,function(result){            
                if(result == "0"){
                    $('#resultMsg').attr('class','errorMsg');
                }
                else{
                    alert(result);
                    $('[name=pointNo]', $(this).parents(".bill")).val(no);
                }
    
            });
        }
    });
    

    });