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;
        }
    });
});
6
  • i think $("#txtName").autocomplete() is sufficient no need for keyDown Commented Sep 15, 2014 at 11:08
  • What does the MVC action look like? Commented Sep 15, 2014 at 11:08
  • You just want it to begin searching on the first keystroke? Try using minLength: 1. Commented Sep 15, 2014 at 11:08
  • @SandipPingle $("#txtName").autocomplete() is working. But i want functionality like, when i first click on textbox and press key down, then all the results should come Commented Sep 15, 2014 at 11:10
  • @AshleyMedway Here i am using Url.Action. so Home is my controller and GetAllName is ActionName Commented Sep 15, 2014 at 11:11

2 Answers 2

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

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

1 Comment

i have gone through code in given url. and that was what i wanted to do. Now it is working fine.
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;
    }
});

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.