Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Oxford, UK
    Posts
    422
    Thanks
    14
    Thanked 27 Times in 27 Posts

    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!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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.
    Last edited by abduraooft; 02-10-2009 at 09:42 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    brazenskies (02-10-2009)

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Oxford, UK
    Posts
    422
    Thanks
    14
    Thanked 27 Times in 27 Posts
    perfect thanks! and when I get a chance I'll have a good read!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •