0

I have this function that gets a list of values back as an array from a web service and wires that list up to the autocomplete of some inputs (we don't know how many qualifying inputs will be on the page when the function runs). I've verified that I'm getting an array back from the web service call (JSON that gets parsed into an Array object). I'm thinking something must be wrong with the code that attaches to autocomplete. Any ideas?

function getDepartmentList() {
    var dUrl = "/Service/Departments/?uid=" + userId;

    $.ajax({
        url: dUrl,
        cache: false,
        success: function (data) {
            $("[id$=__Department]").die();
            $("[id$=__Department]").live("keyup.autocomplete", function () {
                $(this).autocomplete({
                    source: data
                });
            });
        },
    });
}
SuperNES
  • 2,760
  • 9
  • 37
  • 49
  • Yes, generally you don't need to attach autocomplete more than once. just change the source option. – Kevin B Aug 22 '13 at 19:10
  • That's what the .die() is for... detaching it and reattaching it to ensure that only one is attached at a given time. These get added to the page dynamically and there's no better way to wire them up. – SuperNES Aug 22 '13 at 19:12
  • right... but... that tells me that you are calling .autocomplete on the same element more than once, which is what i'm talking about. – Kevin B Aug 22 '13 at 19:14
  • If I set a breakpoint in the code on $(this).autocomplete, I only hit it once... – SuperNES Aug 22 '13 at 19:16
  • See here... this basic setup was working before. http://stackoverflow.com/questions/4551230/bind-jquery-ui-autocomplete-using-live – SuperNES Aug 22 '13 at 19:18
  • you don't hit it every time you keyup? place a console.log there. I guarantee you're hitting it every time you key up on one of those inputs. The answer that you linked to has the same problem. – Kevin B Aug 22 '13 at 19:19
  • Dan's answer in that question is the correct way to initiate an autocomplete input dynamically. – Kevin B Aug 22 '13 at 19:25

0 Answers0