0

I have the following code to add autocompletion to my textBox, autocompletion shows up and everything works, but when a user selected an item the select callback does not get called? Any solutions?

$(function() {

$("#txtItem").autocomplete({
    minLength: 1,
    source: function (request, response) {
        $.ajax({
            url: "/Correct url here",
            dataType: "json",
            data: {
                term: request.term
            },
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    }
                }))
            },
            select: function (event, ui) {
                alert("sdfsdf");
                //$("#txtItemId").val(ui.item.value.ItemId);
            }
        })
    }
})

}
2
  • 1
    Your select is a parameter to your ajax call, not to your auto-complete call. Commented Mar 11, 2013 at 18:11
  • @cjc343 Can't believe that I spent 2 hours trying to find that out. Thanks Commented Mar 11, 2013 at 18:16

2 Answers 2

2

rewrite the code to be like this :

$(function() {

$("#txtItem").autocomplete({
    minLength: 1,
    source: function (request, response) {
        $.ajax({
            url: "/Correct url here",
            dataType: "json",
            data: {
                term: request.term
            },
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    }
                }))
            }            
        })
    },
    select: function (event, ui) {
        alert("sdfsdf");
        //$("#txtItemId").val(ui.item.value.ItemId);
    }
})

}

let me know if its worked

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

2 Comments

Still missing a bunch of semicolons.
@cjc343 But its still valid Javascript
2
$(function() {

$("#txtItem").autocomplete({
minLength: 1,
source: function (request, response) {
    $.ajax({
        url: "/Correct url here",
        dataType: "json",
        data: {
            term: request.term
        },
        success: function (data) {
            response($.map(data, function (item) {
                return {
                    label: item.Name,
                    ItemId: item.ItemId//this is will put item id in memory
                }
            }))
        }            
    })
},//this is after the source property select is an auto complete property many examples screw this up and it is in side the ajax block of code
select: function (event, ui) {
    alert("sdfsdf");
    //$("#txtItemId").val(ui.item.value.ItemId);
  }
  })}

here is a working example http://plnkr.co/edit/RbuRDh1xRIwyO1QamvCe?p=preview

Comments

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.