Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-10-2009, 09:27 AM   PM User | #1
brazenskies
Regular Coder

 
Join Date: May 2008
Location: Oxford, UK
Posts: 422
Thanks: 14
Thanked 27 Times in 27 Posts
brazenskies is on a distinguished road
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!
brazenskies is offline   Reply With Quote
Old 02-10-2009, 09:38 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 02-10-2009 at 09:42 AM..
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
brazenskies (02-10-2009)
Old 02-10-2009, 10:07 AM   PM User | #3
brazenskies
Regular Coder

 
Join Date: May 2008
Location: Oxford, UK
Posts: 422
Thanks: 14
Thanked 27 Times in 27 Posts
brazenskies is on a distinguished road
perfect thanks! and when I get a chance I'll have a good read!
brazenskies is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:17 PM.


Advertisement
Log in to turn off these ads.