jquerykey-events

Jquery: how to trigger click event on pressing enter key


I need to execute a button click event upon pressing key enter.

As it is at the moment the event is not firing.

Please Help me with the syntax if possible.

$(document).on("click", "input[name='butAssignProd']", function () {
   //all the action
});

this is my attempt to fire click event on enter.

$("#txtSearchProdAssign").keydown(function (e) {
  if (e.keyCode == 13) {
    $('input[name = butAssignProd]').click();
  }
});

Solution

  • try out this....

    $('#txtSearchProdAssign').keypress(function (e) {
     var key = e.which;
     if(key == 13)  // the enter key code
      {
        $('input[name = butAssignProd]').click();
        return false;  
      }
    });   
    

    $(function() {
    
      $('input[name="butAssignProd"]').click(function() {
        alert('Hello...!');
      });
    
      //press enter on text area..
    
      $('#txtSearchProdAssign').keypress(function(e) {
        var key = e.which;
        if (key == 13) // the enter key code
        {
          $('input[name = butAssignProd]').click();
          return false;
        }
      });
    
    });
    <!DOCTYPE html>
    <html>
    
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
      <meta charset=utf-8 />
      <title>JS Bin</title>
    </head>
    
    <body>
      <textarea id="txtSearchProdAssign"></textarea>
      <input type="text" name="butAssignProd" placeholder="click here">
    </body>
    
    </html>


    Find Demo in jsbin.com