Using the onkeypress event

In the following code which runs my serach box on the page - I'm trying to make it so that if a users hits say a key on their keyboard say the enter key, the serach fires instead of only working if the user hits the search button - The below code works fine if I hit the serach button but not on the onkeypress event - if say - I hit a keystroke after I type in my serach term.  

document.write('<table border=0 width=768 cellpadding=0 cellspacing=0 valign=bottom><tr bgcolor="#FFFFF"><td valign=top bgcolor="#FFFFFF" rowspan="3"><!--<form name="frmSearchSite" method="get" action="/searchresults.asp">--><input type=text size=12 name="qu" id="quSearch"><input type=image src="/Images/search.jpg" name="search" width="30" height="9" border=0 onclick="window.location.href=\'http://www.mysite.com/searchresults.asp?qu=\'+ document.getElementById(\'quSearch\').value; return false;" onkeypress="onclick="window.location.href=\'http://www.mysite.com/searchresults.asp?qu=\'+ document.getElementById(\'quSearch\').value; return false;"><br><br><!--</form>--> </td></tr><tr><td valign=bottom bgcolor="#FFFFFF" height="51" width="196" colspan="2</td></tr></table>')
dylanoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KennyTMCommented:
maybe removing the [onclick="] immediately after [onkeypress="]?
0
dylanoneAuthor Commented:
Even after fixing that - I still get the same behavior - the page still calls itself instead of going to the URL string
0
dylanoneAuthor Commented:
This is what I have now:

document.write('<table border=0 width=768 cellpadding=0 cellspacing=0 valign=bottom><tr bgcolor="#FFFFF"><td valign=top bgcolor="#FFFFFF" rowspan="3"><!--<form name="frmSearchSite" method="get" action="/searchresults.asp">--><input type=text size=12 name="qu" id="quSearch"><input type=image src="/Images/search.jpg" name="search" width="30" height="9" border=0 onclick="window.location.href=\'http://www.mysite.com/searchresults.asp?qu=\'+ document.getElementById(\'quSearch\').value; return false;" onkeypress="window.location.href=\'http://www.mysite.com/searchresults.asp?qu=\'+ document.getElementById(\'quSearch\').value; return false;"><br><br><!--</form>--> </td></tr><tr><td valign=bottom bgcolor="#FFFFFF" height="51" width="196" colspan="2</td></tr></table>')
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

smaccariCommented:
Kenny is right, and here is the code for handling the enter keypress:

document.write('<table border=0 width=768 cellpadding=0 cellspacing=0 valign=bottom><tr bgcolor="#FFFFF"><td valign=top bgcolor="#FFFFFF" rowspan="3"><form name="frmSearchSite" method="get" action="/searchresults.asp"><input type=text size=12 name="qu" id="quSearch"><input type=image src="/Images/search.jpg" name="search" width="30" height="9" border=0  onkeypress="if (((window.event)&&(event.keyCode==13))||(e.which==13))document.forms[\'frmSearchSite\'].submit();"></form><br><br></td></tr><tr><td valign=bottom bgcolor="#FFFFFF" height="51" width="196" colspan="2"></td></tr></table>')
0
KennyTMCommented:
Well, if you wrap everything with a <form> and assign the code to the "onsubmit" event then this is not a problem because most browser will accept the [Enter] key as the same as submitting.

The following code works for me:

<form onsubmit="window.location.href='http://www.mysite.com/searchresults.asp?qu='+ document.getElementById('quSearch').value; return false;">
<table border="0" width="768" cellpadding="0" cellspacing="0" valign="bottom">
<tr bgcolor="#FFFFFF">
<td valign=top bgcolor="#FFFFFF" rowspan="3">
<input type=text size=12 name="qu" id="quSearch">
<input type="image" src="/Images/search.jpg" name="search" width="30" height="9" border="0" />
 <br><br></td></tr><tr><td valign=bottom bgcolor="#FFFFFF" height="51" width="196" colspan="2"></td></tr></table>
</form>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KennyTMCommented:
In fact you don't need Javascript in this situation, just HTML is enough:

<form action="http://www.mysite.com/searchresults.asp" method="GET">
<table border="0" width="768" cellpadding="0" cellspacing="0" valign="bottom">
<tr bgcolor="#FFFFFF">
<td valign="top" bgcolor="#FFFFFF" rowspan="3">
<input type="text" size="12" name="qu" id="quSearch">
<input type="image" src="/Images/search.jpg" name="search" width="30" height="9" border="0" />
 <br><br></td></tr><tr><td valign="bottom" bgcolor="#FFFFFF" height="51" width="196" colspan="2"></td></tr></table>
</form>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.