I've provided 2 examples here, the first one using jQuery while the second uses old school javascript.
jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$("document").ready(function(){
$(".searchBox").keypress(function(e){
if (e.keyCode == 13)
//add code here
});
});
</script>
<div class="searchBox">
<input type="text" name="searchText"/>
<input type="submit" value="search now" />
</div>
Pure javascript
I've done this before using the "onkeypress" action on a div that surrounds the form. This was tested in IE7, Firefox3, and Chrome.
here's an example:
<div id="searchBox" onkeypress="return clickButton(event,'btnSearch')">
<input name="searchText" type="text" id="searchText" />
<input type="button" name="btnSearch" value="Search" id="btnSearch" onClick="SubmitAction()" />
</div>
<script type="text/javascript" language="javascript">
function SubmitAction()
{
alert('button clicked');
}
function clickButton(e, btId){
var bt = document.getElementById(btId);
if (typeof bt == 'object'){
if(navigator.appName.indexOf("Netscape")>(-1)){
if (e.keyCode == 13){
bt.click();
return false;
}
}
if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){
if (event.keyCode == 13){
bt.click();
event.cancelBubble = true;
return false;
}
}
}
}
</script>
You may have to edit this to make it fully compatible across all browsers but you should get the idea from it. Essentially any keypress while this div is focussed will call the clickButton() function. As mentioned by other answers the e.keyCode of 13 represents the Enter keypress.