Search code examples
javascriptjqueryjquery-uijquery-eventsjquery-ui-autocomplete

How to fire click event on pressing enter Jquery-UI autocomplete


I have following JS:

  $("#search").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Home/GetCompanyNames",
                    dataType: "json",
                    data: "searchterm=" + request.term,
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.Name,
                                value: item.Name,
                                LogoUrl: item.LogoUrl,
                                InternetName :item.InternetName,
                                Name: item.Name
                            };
                        }));
                    }
                });
            },
          minLength: 1
        }).data("autocomplete")._renderItem = function (ul, item) {
            var innerHtml = '<a href=/Store/'+item.InternetName+'><div class="list_item_container" style="height: 60px;"><div class="image" style="float: left; margin-right: 35px;"><img src="' + item.LogoUrl + '"></div><div class="label">' + item.Name + '</div></div></a>';
            return $("<li></li>")
        .data("item.autocomplete", item)
        .append(innerHtml)
        .appendTo(ul);
        };

    });

which renders this

enter image description here

This is a suggestion dropdown which shows when I enter any character in textbox like ib in this case,
Everything is working fine but there is only one problem when I click any item of dropdown with mouse it take me to the url which is written in its anchor tag, I want that if I scroll down with and press enter on some item it will do the same currently it's not doing anything when I press enter on a item of dropdown.


Solution

  • Take a look at this jqFAQ.com topic, this will help you to navigate to the url on selection made by both mouse click and pressing enter. There are few more autocomplete related FAQs too.