Automatically set input value and open offers - Google Autofill

I have a Google Autofill enabled input field. I would like to fill this field with some default values, focus the input and automatically open the forecast window below the input.

I obviously have no problem setting the input value and focus, but I cannot find a way to open the suggestion window. It opens at any press of a button; in any case, the simulation of a keypress event at the input did not work.

So, I am wondering if there is a way to trigger an event that the autocomplete window does.

I have the following code example:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> <input type="text" id="q" /> 

JS:

 var input = document.getElementById('q'); var autocomplete = new google.maps.places.Autocomplete(input); $('#q').val('Rome, ').focus(); // how to open suggestions box ? 
+4
source share
1 answer

It is not possible to do this via js or jQuery, because the pragmatic use of event-triggering functions is not the same as when triggering your own DOM event.

This Fiddle shows the difference between the embedded DOM event and the jQuery event trigger, you will see the difference between the execution of the code and the click on the link.

Also in this Fiddle I updated your code by changing 'Rome, ' so that it mimics the stroking of the keys but it doesn’t make any changes, but when you press the key, it works.

You can also check these links:

0
source

Source: https://habr.com/ru/post/1479242/


All Articles