AJAX: How do I default an enter-press to a non-submit button?

sonotleet

New member
I'd like to make a search bar were the user can press enter, and the results are posted without having to refresh the page.

If I make the "search" button a submit button, the page will refresh.

If I make the "search" button a regular button, the page will load the results without refreshing. But the user can't use the enter key to search.

Any ideas?
 
In the input field put an onkeyup attribute that checks for what key was just pressed:

<input type="text" onkeyup="if(event.keyCode==13) ajaxFunc()">

keyCode 13 is the Enter key.
Replace "ajaxFunc()" with the function that runs your Ajax call.
 
Back
Top