Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've provided 2 examples here, the first one using jQuery while the second uses old school javascript.</p> <p><strong>jQuery</strong></p> <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script language="javascript" type="text/javascript"&gt; $("document").ready(function(){ $(".searchBox").keypress(function(e){ if (e.keyCode == 13) //add code here }); }); &lt;/script&gt; &lt;div class="searchBox"&gt; &lt;input type="text" name="searchText"/&gt; &lt;input type="submit" value="search now" /&gt; &lt;/div&gt; </code></pre> <p><strong>Pure javascript</strong></p> <p>I've done this before using the "onkeypress" action on a div that surrounds the form. This was tested in IE7, Firefox3, and Chrome.</p> <p>here's an example:</p> <pre><code>&lt;div id="searchBox" onkeypress="return clickButton(event,'btnSearch')"&gt; &lt;input name="searchText" type="text" id="searchText" /&gt; &lt;input type="button" name="btnSearch" value="Search" id="btnSearch" onClick="SubmitAction()" /&gt; &lt;/div&gt; &lt;script type="text/javascript" language="javascript"&gt; function SubmitAction() { alert('button clicked'); } function clickButton(e, btId){ var bt = document.getElementById(btId); if (typeof bt == 'object'){ if(navigator.appName.indexOf("Netscape")&gt;(-1)){ if (e.keyCode == 13){ bt.click(); return false; } } if (navigator.appName.indexOf("Microsoft Internet Explorer")&gt;(-1)){ if (event.keyCode == 13){ bt.click(); event.cancelBubble = true; return false; } } } } &lt;/script&gt; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload