javascript - How can I make my Google Maps api v3 address search bar work by hitting the enter button on the keyboard? -


i'm developing webpage , make more user friendly. have functional google maps api v3 , address search bar. currently, have use mouse select search initialize geocoding function. how can make map return placemark hitting enter button on keyboard , clicking search button? want make user-friendly possible.

here javascript , div, respectively, created address bar:

var geocoder;     function initialize() {     geocoder = new google.maps.geocoder ();      function codeaddress () {         var address = document.getelementbyid ("address").value;         geocoder.geocode ( { 'address': address}, function(results, status)  {         if (status == google.maps.geocoderstatus.ok)  {             map.setcenter(results [0].geometry.location);             marker.setposition(results [0].geometry.location);             map.setzoom(14);             }          else {             alert("geocode not successful following reason: " + status);                 }         });                              }                function initialize() {         document.getelementbyid("address").focus(); }     function setfocusonsearch() {         document.getelementbyid("search").focus(); }     function codeaddress() {         document.getelementbyid("address").focus(); }         <body onload="initialize()">                     <div id="geocoder">         <input id="address" type="textbox" value="" "onblur="setfocusonsearch()">         <input id="search" type="button" value="search" onclick="codeaddress()">     </div>                   </body> 

thank in advance help

enter image description here

you want 3 easy steps:

1) wait dom loaded / initialize map (you did already)

<body onload="initialize()"> 

then within initialize function:

2) set focus address field

document.getelementbyid('address').focus(); 

3) listen keyup event of address field , catch enter key code / call codeaddress function

// bind key-up event listener address field document.getelementbyid("address").addeventlistener('keyup', function (event) {      // check event key code     if (event.keycode == 13) {          // key code 13 == enter key pressed (and released)         codeaddress();     } }); 

either use onlick / onkeyup html event attributes or add event listeners javascript don't both.

jsfiddle demo


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -