1

How to bind click(from Button1's click event) event in textbox When I pressed the enter key

PsyGnosis
  • 1,489
  • 6
  • 21
  • 28
  • 2
    possible duplicate of [Trigger button click with JavaScript on Enter key in Text Box](http://stackoverflow.com/questions/155188/trigger-button-click-with-javascript-on-enter-key-in-text-box) – Nanne Apr 29 '11 at 12:36

2 Answers2

12
$('#idoftextbox').keypress(function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        //enter has been pressed
    };
});
alexl
  • 6,841
  • 3
  • 24
  • 29
  • Some other Char Codes you may be interested in: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx – Seth Apr 29 '11 at 12:36
  • I think jQuery normalizes `event.which`, so the `var code=...` line should not be needed. – kapa Apr 29 '11 at 12:57
3
<input type="text" name="textbox" id="textbox" />

$("#textbox").bind('keypress', function(e)
{
   if(e.which == 13) 
   {
       // enter key was hit, do what you need to do here
   }
});

Since you didn't say what "textbox" means to you (as it can be textarea too), I assumed some dummy markup that I posted and then I bound the event to it. I don't see the reason for binding an event to click and then bind event after that to the element.

Michael J.V.
  • 5,499
  • 1
  • 20
  • 16