google places select first on enter

Solutions on MaxInterview for google places select first on enter by the best coders in the world

showing results for - "google places select first on enter"
Frida
04 Apr 2016
1var selectFirstOnEnter = function(input) {  // store the original event binding function
2    var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
3    function addEventListenerWrapper(type, listener) {  // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected, and then trigger the original listener.
4        if (type == "keydown") { 
5            var orig_listener = listener;
6            listener = function(event) {
7                var suggestion_selected = $(".pac-item-selected").length > 0;
8                if (event.which == 13 && !suggestion_selected) { 
9                    var simulated_downarrow = $.Event("keydown", {keyCode: 40, which: 40}); 
10                    orig_listener.apply(input, [simulated_downarrow]); 
11                }
12                orig_listener.apply(input, [event]);
13            };
14        }
15        _addEventListener.apply(input, [type, listener]); // add the modified listener
16    }
17    if (input.addEventListener) { 
18        input.addEventListener = addEventListenerWrapper; 
19    } else if (input.attachEvent) { 
20        input.attachEvent = addEventListenerWrapper; 
21    }
22}
23