Maddie Graham
Maddie Graham

Reputation: 2177

Initiating autocomplete after click

I am trying to use this plugin in my project to get an auto-complete field. My code looks like this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  } );
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>

I would like the check box to appear immediately after clicking on it. Not after entering the first letter. I wish it was earlier. How can I get this effect?

I try using this:

  $( ".selector" ).autocomplete({
  minLength: 0
  });

But without results.

Upvotes: 0

Views: 67

Answers (1)

ORunnals
ORunnals

Reputation: 131

You need to bind a function to the focus event like so:

<script type="text/javascript">
$(function() {
    $('#id').autocomplete({
        source: ["ActionScript",
                    /* ... */
                ],
        minLength: 0
    }).focus(function(){     
        $(this).data("autocomplete").search($(this).val());
    });
});

You can also see a functioning version here: https://codepen.io/orunnals/pen/VwYrdRg

Additionally this was already answered here and states some differences in some of the answers depending on what versions you're using.

Upvotes: 1

Related Questions