0

I want my button so that when it is clicked, it will also press enter on an input field. I am using Google Maps API, place search box.(https://developers.google.com/maps/documentation/javascript/examples/places-searchbox)

I also added in code so that the API will ask permission for your location. Here is my button and input field code.

HTML

<form>
    <button type="submit" id="go" value="Food near me">Click</button>
    <input id="pac-input" type="text" placeholder="">
</form>

Javascript

document.getElementById('go').onclick = function () {
    document.getElementById('pac-input').value = this.value;
};

As you can see, when the button is pressed, "Food near me" is added to the pac-input input. But in order for it to display the results, I have to press the enter key inside the input field, I want the results to show when I mouse click the button

George
  • 6,630
  • 2
  • 29
  • 36
qaakmnd
  • 339
  • 2
  • 4
  • 18

3 Answers3

0

instead of pressing enter to trigger your textbox function you may create a shared function to be called by enter key on textbox or button click like

function getData(){
 //Get DataHere;
}

function buttonClick(){
 getData();
}


function enterKeyFunc(e){
 if(e.which==13)
 {
  getData();
 }
}

and if you really need to simulate keypress event check this post

Is it possible to simulate key press events programmatically?

Community
  • 1
  • 1
Hasan Elsherbiny
  • 598
  • 2
  • 14
  • 31
0

Enter key pressing:

$("#pac-input").keydown(function(e){
    if(e.which === 13)
    {
        //Do  your button click option
    }
});
User6667769
  • 745
  • 1
  • 7
  • 25
0

jQuery has trigger function to execute all handlers and behaviors attached to the matched elements for the given event type.. http://api.jquery.com/trigger/

kaijun
  • 11,376
  • 1
  • 9
  • 12