1

I am using jQuery jquery-1.10.2 and jQuery ui 1.11.4

Here is the autocomplete function :

$("#txtPOI").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: '<?php echo site_url("crowd/get_POIs") ?>',
            data: {cat: selectedCode, q: request.term},
            dataType: "json",
            type: "post",
            success: function(data) {

                response( $.map( data, function( item )
                {
                    return{
                            label: item.title,
                            value: item.title,
                            contentid: item.contentid,
                            latitude: item.latitude,
                            longitude: item.longitude
                        }
                }));

            },
            fail : function ( jqXHR, textStatus, errorThrown ) {
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            },
            select: function( event, ui ) {
                $("#txtPOI").val(ui.item.latitude);
                alert('selected');
                /*
                alert(ui.item.contentid);
                log( ui.item ?
                  "Selected: " + ui.item.label :
                  "Nothing selected, input was " + this.title);
                alert(ui.item.value);
                */
            },
            open: function(event, ui) {
                $(".ui-autocomplete").css("z-index", 1000);
            },
            minLength: 3

        });
    }
});

The ajax response successfully returned data from database. However when I tried to do something inside the select nothing happened, it seems that the select is not triggered. I would like to get the value of ui.item.contentid as well as ui.item.latitude and ui.item.longitude upon the selection of the autocomplete item.

Why the select event is not triggered ? How to solve this issue ?

5
  • try to remove the $("#txtPOI").val(ui.item.latitude); and test if you get the alert working Commented Sep 28, 2015 at 10:17
  • it's not working. there is no error shown on browser debugger as well Commented Sep 28, 2015 at 10:19
  • can you provide some html and the url of ajax so that i can help you Commented Sep 28, 2015 at 10:23
  • check @Neeraj Verma answer Commented Sep 28, 2015 at 10:30
  • Yeah. it's working now. Thank you Commented Sep 28, 2015 at 10:30

1 Answer 1

7

Select is option for autocomplete object not for Ajax object.

Try this:

$("#txtPOI").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: '<?php echo site_url("crowd/get_POIs") ?>',
            data: {cat: selectedCode, q: request.term},
            dataType: "json",
            type: "post",
            success: function(data) {

                response( $.map( data, function( item )
                {
                    return{
                            label: item.title,
                            value: item.title,
                            contentid: item.contentid,
                            latitude: item.latitude,
                            longitude: item.longitude
                        }
                }));

            },
            fail : function ( jqXHR, textStatus, errorThrown ) {
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    },
    select: function( event ) {
         // do what ever you want to do...
    }
});

Sign up to request clarification or add additional context in comments.

1 Comment

I got my mistake now. Thank you very much for your help. it's working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.