CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Using onkeypress to submit ajax request (http://www.codingforums.com/showthread.php?t=158373)

brazenskies 02-10-2009 09:27 AM

Using onkeypress to submit ajax request
 
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...

Code:

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

Code:

function runSearch(){       
                if (document.frmSearch1.txtCP.value != "")
                        {
                                http = createRequestObject();
                                http.open('get', 'maptools/searchLocation.asp?search=' + document.frmSearch1.txtCP.value );
                                http.onreadystatechange = handleSearchResponse;
                                http.send(null);
                        }
                else
                        {
                                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.

Any help would be appreciated,
Thanks in advance!

abduraooft 02-10-2009 09:38 AM

Try
Code:

<input type="image" onclick="runSearch();return false;" src="images/btnTownSearch.jpg"/>
instead of
Code:

<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 AFAP.

brazenskies 02-10-2009 10:07 AM

perfect thanks! and when I get a chance I'll have a good read!


All times are GMT +1. The time now is 01:38 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.