JQuery autoComplete view all on click?

I use jQuery autocomplete in a relatively simple way:

$(document).ready(function() { var data = [ {text: "Choice 1"}, {text: "Choice 2"}, {text: "Choice 3"} ] $("#example").autocomplete(data, { matchContains: true, minChars: 0, formatItem: function(item) { return item.text; } } ); }); 

How to add an onclick event (for example, a button or link) in which all available autocomplete options will be displayed? I am mainly looking to create a hybrid autocomplete and a select / dropdown element.

Thank!

+59
jquery autocomplete jquery-autocomplete
Aug 12 '09 at 20:26
source share
21 answers

I donโ€™t see an obvious way to do this in the documents, but you are trying to fire a focus (or click) event in a text box with autocomplete enabled:

 $('#myButton').click(function() { $('#autocomplete').trigger("focus"); //or "click", at least one should work }); 
+17
Aug 12 '09 at 20:33
source share

You can trigger this event to show all parameters:

 $("#example").autocomplete( "search", "" ); 

Or see an example in the link below. It seems like exactly what you want to do.

http://jqueryui.com/demos/autocomplete/#combobox

EDIT (from @cnanney)

Note. You must set minLength: 0 in your autocomplete for an empty search string in order to return all elements.

+98
Aug 05 2018-10-10T00:
source share

I found this works best

 var data = [ { label: "Choice 1", value: "choice_1" }, { label: "Choice 2", value: "choice_2" }, { label: "Choice 3", value: "choice_3" } ]; $("#example") .autocomplete({ source: data, minLength: 0 }) .focus(function() { $(this).autocomplete('search', $(this).val()) }); 

It searches for labels and places the value in the $ element (# example)

+34
Mar 28 2018-12-12T00:
source share

To show all elements / simulate combobox behavior, you must first make sure that you set the minLength parameter to 0 (default is 1). You can then bind the click event to perform a search with an empty string.

 $('inputSelector').autocomplete({ minLength: 0 }); $('inputSelector').click(function() { $(this).autocomplete("search", ""); }); 
+6
May 17 '12 at 1:54 pm
source share

from Display jquery ui auto-complete list in focus

The solution is to make it work more than once

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

+4
Dec 29 '11 at 12:32
source share
  $j(".auto_complete").focus(function() { $j(this).keydown(); }) 
+3
Apr 13 '10 at 16:52
source share

try the following:

  $('#autocomplete').focus(function(){ $(this).val(''); $(this).keydown(); }); 

and minLength is set to 0

works every time :)

+3
Apr 05 '11 at 10:15
source share

You must set minLength to zero to make this work! Here is a working example.

 $( "#dropdownlist" ).autocomplete({ source: availableTags, minLength: 0 }).focus(function() { $(this).autocomplete('search', $(this).val()) }); }); 
+3
Sep 20 '16 at 8:10
source share

this shows all parameters: "%" (see below)

The important thing is that you must place it under the previous #example declaration, as in the example below. It took me a while to understand.

 $( "#example" ).autocomplete({ source: "countries.php", minLength: 1, selectFirst: true }); $("#example").autocomplete( "search", "%" ); 
+2
Dec 16 '10 at 18:13
source share

you can use this:

 $("#example").autocomplete( "search", $("#input").val() ); 
+2
Apr 10 2018-11-11T00:
source share

This is the only thing that works for me. The list shows each time and closes when you select:

 $("#example") .autocomplete(...) .focus(function() { var self = this; window.setTimeout(function() { if (self.value.length == 0) $(self).autocomplete('search', ''); }); }) 
+2
Jul 21 2018-11-11T00:
source share

hope this helps someone:

 $('#id') .autocomplete({ source: hints_array, minLength: 0, //how many chars to start search for position: { my: "left bottom", at: "left top", collision: "flip" } // so that it automatically flips the autocomplete above the input if at the bottom }) .focus(function() { $(this).autocomplete('search', $(this).val()) //auto trigger the search with whatever in the box }) 
+2
Nov 15 '13 at 10:30
source share
 <input type="text" name="q" id="q" placeholder="Selecciona..."/> <script type="text/javascript"> //Mostrar el autocompletado con el evento focus //Duda o comentario: http://WilzonMB.com $(function () { var availableTags = [ "MongoDB", "ExpressJS", "Angular", "NodeJS", "JavaScript", "jQuery", "jQuery UI", "PHP", "Zend Framework", "JSON", "MySQL", "PostgreSQL", "SQL Server", "Oracle", "Informix", "Java", "Visual basic", "Yii", "Technology", "WilzonMB.com" ]; $("#q").autocomplete({ source: availableTags, minLength: 0 }).focus(function(){ $(this).autocomplete('search', $(this).val()) }); }); </script> 

http://jsfiddle.net/wimarbueno/6zz8euqe/

+2
Dec 01 '14 at 23:11
source share

I solved this using the minChars: 0 attribute and after, launch two clicks. (autocomplete shows after 1 click on the input) my code

 <input type='text' onfocus='setAutocomplete(this)'> function setAutocomplete(el){ $(el).unbind().autocomplete("./index.php", {autoFill:false, minChars:0, matchContains:true, max:20}); $(el).trigger("click");$(el).trigger("click"); } 
+1
Aug 09 2018-11-11T00:
source share

I saw all the answers that seem complete.

If you want to get a list when the cursor is in the text box OR when you click on the corresponding shortcut, here is how you can do it:

 //YourDataArray = ["foo","bar"]; $( "#YourID" ).autocomplete({ source: YourDataArray }).click(function() { $(this).autocomplete("search", " "); }); 

this works fine in Firefox, IE, Chrome ...

+1
Nov 21
source share
 $("#searchPkgKeyWord").autocomplete("searchURL", { width: 298, max: 1000, selectFirst: false }).result(function (event, row, formatted) { callback(row[1]); }).focus(function(){ $(this).click(); //once the input focus, all the research will show }); 
+1
Apr 28 '14 at 2:42 on
source share

I could not force part of $("#example").autocomplete( "search", "" ); only work when I change my search with the symbol that exists in my source, it works. So I then used, for example, $("#example").autocomplete( "search", "a" ); .

0
Nov 25 '10 at 9:50
source share

I think the best option is to put $ ("# idname"). autocomplete ("search", ""); into the onclick parameter of the text field. Since when selected, focus is placed in jquery, this can be a workaround. I donโ€™t know if it should be an acceptable solution.

0
Mar 31 '12 at 10:43
source share

I needed to do this recently, and after trying several different permutations (using onfocus, onclick of textbox, etc.), I finally settled on this ...

  <input id="autocomplete" name="autocomplete" type="text" value="Choose Document"> <p> <button type="submit" value="Submit" name="submit" id="submit" > Submit </button> </p> <script type="text/javascript"> $("#autocomplete").autocomplete( { source: '@Url.Content("~/Document/DocumentTypeAutoComplete/")' //<--ddl source , minLength: 0 // <-- This is necessary to get the search going on blank input , delay: 0 , select: function (event, ui) { if (ui.item) { $("#autocomplete").val(ui.item.value);//<-- Place selection in the textbox $("docForm").submit(); loadDocTypeCreatePartial(ui.item); $("#submit").focus(); //This stops the drop down list from reopening // after an item in the select box is chosen // You can place the focus anywhere you'd like, // I just chose my *submit* button } } }).focus(function () { // following line will autoselect textbox text // making it easier for the user to overwrite whats in the // textbox $(this).select(); //The below line triggers the search function on an empty string $(this).data("autocomplete").search(''); }); </script> 

This opens the ddl autocomplete list in focus (even if you have the default text in the input field as shown above).

It also automatically selects text in the text box so that the user cannot clear the text.

As soon as an element is selected from the autocomplete list, it places this element in the autocomplete input field and moves focus to another control (thus preventing the autocomplete from reopening).

I plan to replace it with adding dynamic Ajax calls to the very nice Chosen pick lists with Refreshing Melting Ice when I get a chance.

0
Dec 10 '12 at 2:05
source share

I used this method:

 $("#autocomplete").autocomplete({ source: YourDataArray, minLength: 0, delay: 0 }); 

Then

 OnClientClick="Suggest(this); return false;"/> function Suggest(control) { var acControl = $("#" + control.id).siblings(".ui-autocomplete-input"); var val = acControl.val(); acControl.focus(); acControl.autocomplete("search"); 
0
Jan 27 '16 at 6:24
source share

You can also use the search function without parameters:

 jQuery("#id").autocomplete("search", ""); 
0
Apr 25 '19 at
source share



All Articles