jquerybindenter

jquery submit using button or enter?


I have an input field and a button on my form, currently I using the button to start a search.

Can I use the button and if the user presses 'Enter' on the form field ?

This is the search input field and button:

<input id='search' name='search' />
<input type='button' name='find' id='find' value='Find'>

and this is the Jquery I'm using for the button.

$('body').on('click', '#find', function(){

How do I add the enter key on the search field as well ?

Thanks


Solution

  • this works with button and pressing "enter"

    <form id="myForm">
        <input id='search' name='search' />
        <input type='submit' value='Find'>
    </form>
    
    $("#myForm").submit(function( event ) {
      event.preventDefault();
    // your code here
    });