Trigger a button click with JavaScript on the Enter key in a text box

Figured this out:

<input type="text" id="txtSearch" onkeypress="return searchKeyPress(event);" />
<input type="button" id="btnSearch" Value="Search" onclick="doSomething();" />

function searchKeyPress(e)
    // look for window.event in case event isn't passed in
    e = e || window.event;
    if (e.keyCode == 13)
        return false;
    return true;


No comments yet.

Leave a Reply