Solved

Javascript submit on enter error

Posted on 2010-09-23
7
309 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
  • 4
  • 2
7 Comments
 
LVL 51

Expert Comment

by:HainKurt
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now