Trigger button click with JavaScript on Enter key in Text Box

Posted by ziondreams on Stack Overflow See other posts from Stack Overflow or by ziondreams
Published on 2008-09-30T21:32:09Z Indexed on 2010/05/08 19:58 UTC
Read the original article Hit count: 222

Filed under:
|
|

I have one text input and one button (see below). How can I use JavaScript to trigger the button's click event when the Enter key is pressed inside the text box?

There is already a different submit button on my current page, so I can't simply make the button a submit button. And, I only want the enter key to click this specific button if it is pressed from within this one text box, nothing else.

<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />

Update: I'd like to accept my own answer below, but since that is now allowed I will accept the next best solution.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html