Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a solution that does not make a geocoding request that may return an incorrect result: <a href="http://jsfiddle.net/amirnissim/2D6HW/">http://jsfiddle.net/amirnissim/2D6HW/</a> <br/></p> <p>It simulates a <code>down-arrow</code> keypress whenever the user hits <code>return</code> inside the autocomplete field. The <kbd>↓</kbd> event is triggered before the <kbd>return</kbd> event so it simulates the user selecting the first suggestion using the keyboard.<br/></p> <p>Here is the code (tested on Chrome and Firefox) :<br/></p> <pre><code>&lt;script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'&gt;&lt;/script&gt; &lt;script src="https://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"&gt;&lt;/script&gt; &lt;script&gt; var pac_input = document.getElementById('searchTextField'); (function pacSelectFirst(input) { // store the original event binding function var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent; function addEventListenerWrapper(type, listener) { // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected, // and then trigger the original listener. if (type == "keydown") { var orig_listener = listener; listener = function(event) { var suggestion_selected = $(".pac-item-selected").length &gt; 0; if (event.which == 13 &amp;&amp; !suggestion_selected) { var simulated_downarrow = $.Event("keydown", { keyCode: 40, which: 40 }); orig_listener.apply(input, [simulated_downarrow]); } orig_listener.apply(input, [event]); }; } _addEventListener.apply(input, [type, listener]); } input.addEventListener = addEventListenerWrapper; input.attachEvent = addEventListenerWrapper; var autocomplete = new google.maps.places.Autocomplete(input); })(pac_input); &lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload