tags:

views:

43

answers:

3

Hello everyone, I have a search box and a buttion. currently a user enter some text and press the search button. But I want to add another feature that instead of clicking the search button people can hit enter to search. How can I do that?

Here is my code sample:

<form method="post" action="">

     <input id="search" name="search" type="text" />
     <input id="search_btn" name="search_btn" type="submit" />

</form>

Thanks in advance

+2  A: 

Pressing 'enter' would submit most forms without hocus pocus.

Thorpe Obazee
A: 

You can do it using following ways

<script type="text/javascript">
  function submitenter(myfield,e)
  {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13)
    {
        myfield.form.submit();
        return false;
    }
    else
     return true;
  }

</script>

<form method="post" action="">

     <input id="search" name="search" type="text" :onKeyPress=>"return submitenter(this, event);"/>
     <input id="search_btn" name="search_btn" type="submit"  />

</form>
Salil
-1 for invalid syntax, a badly applied event model and a totally redundant approach
Gareth
A: 
AungAung