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

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

<pre><code>&lt;input type="text" id="txtSearch" /&gt;
&lt;input type="button" id="btnSearch" value="Search" onclick="doSomething();" /&gt;
</code></pre>

Make the button a submit element, so it'll be automatic.
```
```
<input type = "submit"
       id = "btnSearch"
       value = "Search"
       onclick = "return doSomething();"
/>
```
```
Note that you'll need a
```
<form>
```
element containing the input fields to make this work (thanks Sergey Ilinsky).
It's not a good practice to redefine standard behaviour, the
Enter
key should always call the submit button on a form.