0

I want jQuery Autocomplete on textbox key down event

I have tried below code, but it is not working.

$("#txtName").keyDown({
    $("#txtName").autocomplete({
        source: '@Url.Action("GetAllName", "Home")',
        select: function (event, ui) {
            $("#txtName").val(ui.item.value);
            return false;
        }
    });
});
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
User5590
  • 1,383
  • 6
  • 26
  • 61

2 Answers2

4

You do not need the keyDown event otherwise you are rebinding the autocomplete on every keydown.

$("#txtName").autocomplete({
    source: '@Url.Action("GetAllName", "Home")',
    select: function (event, ui) {
        $("#txtName").val(ui.item.value);
        return false;
    }
});

To show all results when one character is entered add minLength: 1

If what you want is to have all the items displayed when the textbox has focus then this is what you are looking for: https://stackoverflow.com/a/4604300/1398425

Community
  • 1
  • 1
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
0

If you just want it to begin searching on the first keystroke, try using minLength: 1:

$("#txtName").autocomplete({
    source: '@Url.Action("GetAllName", "Home")',
    minLength: 1,
    select: function (event, ui) {
        $("#txtName").val(ui.item.value);
        return false;
    }
});
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
DevlshOne
  • 8,357
  • 1
  • 29
  • 37