View Full Version : Using onkeypress to submit ajax request

02-10-2009, 10:27 AM
I'm new to ajax and having a bit of trouble with the 'onkeypress' event.

Here's the scenario...

User enters a search value into a text box, then clicks on an image to submit the request.

Image code...

<img src="images/btnTownSearch.jpg" alt="Submit Search" style="margin-top:4px;" onclick="runSearch()" />

function code...

function runSearch(){
if (document.frmSearch1.txtCP.value != "")
http = createRequestObject();
http.open('get', 'maptools/searchLocation.asp?search=' + document.frmSearch1.txtCP.value );
http.onreadystatechange = handleSearchResponse;
alert("Please choose a centre point")

This works perfectly if you click on the image but I somehow need to modify the code to allow the request to be made when the user hits the 'enter' key.

Also, if it helps at all then here's a link to my page. (http://mapsinternational.co.uk/custom-wall-maps.asp)

Any help would be appreciated,
Thanks in advance!

02-10-2009, 10:38 AM
<input type="image" onclick="runSearch();return false;" src="images/btnTownSearch.jpg"/> instead of

<img onkeypress="return runSearch()" onclick="runSearch()" style="margin-top: 4px;" alt="search!" src="images/btnTownSearch.jpg"/>

There may not be a point in your current case to consider users who have no javascript support, but you must try to adopt the progressive enhancement strategy (http://en.wikipedia.org/wiki/Progressive_enhancement) AFAP.

02-10-2009, 11:07 AM
perfect thanks! and when I get a chance I'll have a good read!