Jquery Autocomplete suggestion names displaying as empty

151 Views Asked by At

Im trying to create autocomplete text box, I have an issue with my jquery. Please correct me where im missing.

  1. Names are not displaying in the suggestion list
  2. When i select the name, i need to place the cityid for the selected city in html hidden input field.
  3. Is there any way to change the suggestion list design.

Here is the code I have tried:

   <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

  <script>
            $(function () {
            var arr_text = [{"cityid":20,"city":"city1"},{"cityid":21,"city":"city2"},{"cityid":22,"city":"city3"}]

            $("#txtauto").autocomplete({
                source: function (request, response) {
                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");

                    var matching = $.grep(arr_text, function (value) {
                        var name = value.city;
                        var id = value.cityid;

                        return matcher.test(name) || matcher.test(id);
                    });
                    response(matching);
                }
            });



        });
  </script>
</head>
<body>

 <div class="ui-widget">
        <form>
            <label for="txtauto">Autocomplete</label>
            <input id="txtauto" />
        </form>
    </div>
</body>
</html>

Here is the output for my codeHere is the output for above code

1

There are 1 best solutions below

0
Anilkumar Amrutham On

Hi Use response like below.

response( $.map( matching, function( item ) {
                        return {
                            label: item.city,
                            value: item.cityid
                        }
                    }));