6

I have an input field with google autocomplete attached on it. I'd like to populate this field with some default values, focus the input and automatically open the prediction box below the input.

I obviously have no problem setting the input value and focus, but I can't find a way to open the suggestions box. It opens on any user keypress; anyway, simulating the keypress event on the input didn't work.

So I'm wondering if there's a way to trigger the event that makes the autocomplete box show up.

I have the following example code:

<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 ?
Lorenzo Marcon
  • 8,029
  • 5
  • 38
  • 63

1 Answers1

0

It’s not possible to do that through js or jQuery because pragmatical use of event triggering functions is not as same as firing the native DOM event.

This Fiddle shows the difference between native DOM event and jQuery event trigger, you’ll see the difference between executing the code and clicking the link.

Also in this Fiddle I updated your code by altering 'Rome, 'so that it simulates key-stroking but it made no changes, but when you hit a key it works.

You may check these links too:

Community
  • 1
  • 1
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47