skip to Main Content

When I press search button my JS code will work.
But when I use enter to submit form Bellow code will not work.
Use of code in magento.
Html

<form action="<?php echo $this->getUrl('storelocator/index/searchbydistance') ?>" method="GET" id="search_by_distance">
<button type="button" onclick="storeSearch.submit(this)"><?php echo $this->__('Search') ?></button></form>

Javascript

<script type="text/javascript">
//<![CDATA[
storeSearch.submit = function(button, url) { 
alert('hi');
}.bind(storeSearch);
//]]>

2

Answers


  1. For the enter keypress form submission behaviour to work you need to have submit button within the <form> element. As all your current button does is submit the form through JS, you could simply change its type:

    <form action="<?php echo $this->getUrl('storelocator/index/searchbydistance') ?>" method="GET" id="search_by_distance">
      <button type="submit"><?php echo $this->__('Search') ?></button>
    </form>
    
    <script type="text/javascript">
      storeSearch.submit = function(button, url) { 
        alert('hi');
      }.bind(storeSearch);
    </script>
    
    Login or Signup to reply.
  2. give some id to to button and then use code like following:

        document.getElementById("id_of_button")
        .addEventListener("keyup", function(event) {
        event.preventDefault();
        if (event.keyCode == 13) {
            document.getElementById("id_of_button").click();
    

    //OR submit your form.

        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search