Solved

Javascript submit on enter error

Posted on 2010-09-23
7
315 Views
Last Modified: 2012-06-27
Ok...  Hopefully a simple fix...  I use this script to allow users to submit their search forms when they hit the enter key in the zip code field...  The problem I'm having is that if they have autocomplete on, and they type 43 and then the autocomplete shows 43123 under the field and the user clicks it and hits enter...  When it submits the search, it only registers the "43".  The autocomplete works fine without my script, but then it only submits if they hit submit...

Thanks...
onKeyPress="return submitenter(this,event)"


<SCRIPT TYPE="text/javascript">
<!--
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
        formMyForm = document.getElementById("home_search_form");
        formMyForm.action = "vehicle_search.php";
        formMyForm.submit();
   }
else
   return true;
}
//-->
</SCRIPT>

Open in new window

0
Comment
Question by:stephenmp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33750155
add this on top, it maybe what you are looking for

if (myfield.length < 5) return false;

user will need to enter again to submit
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33750312
Try onkeyup instead onkeypress
0
 

Author Comment

by:stephenmp
ID: 33750321
That's more a validator... I can do that with jquery...  I'm looking to move the onkeypress to something that will only call on enter when the autocomplete enter has been pushed...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:leakim971
ID: 33750327
Else try a timer :


<SCRIPT TYPE="text/javascript">
<!--
	function submitenter(myfield,e) {
		var keycode = (window.event)?(window.event.keyCode):(e.which);
		if(keycode == 13) setTimeout("afterHalf()", 500);
		return true;
	}
	function afterHalf() {
		formMyForm = document.getElementById("home_search_form");
		formMyForm.action = "vehicle_search.php";
		formMyForm.submit();
	}
//-->
</SCRIPT>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33750519
Thanks for the points!
0
 

Author Comment

by:stephenmp
ID: 33750535
Thank you...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33750536
You're very welcome!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question