document catch enter key

HI experts

i would like to submit the do a javascript action when the user press the 'enter' key while on my page .. even if the user doesn't press enter in a form field.

LVL 2
morpheus3gAsked:
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.

Chinmay PatelChief Technology NinjaCommented:
Hi There,

Add this:
<BODY onkeypress="SubmitMyForm()">

and this to your <HEAD> tag:

<SCRIPT>
function SubmitMyForm()
{
if (event.enterKey="13")
   {
      document.all.YourForm.Submit();
   }
}
</SCRIPT>

Regards,
Chinmay

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
ZvonkoSystems architectCommented:
(only for the correctness of the PAQ)
The correct submit method name is this:
       document.YourFormName.submit();

Or to submit the first or only form on the page:
       document.forms[0].submit();

Chinmay PatelChief Technology NinjaCommented:
Hi There,

I dont remember when, but somewhere... in the past.. I had to use document.all.
that's why when I am not sure about the browser version.. I use it :)
tnx for your input though

Regards,
Chinmay
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

ZvonkoSystems architectCommented:
The problem was not document.all, but the .Submit() call. It has to be .submit()
And document.all is a collection in IE where you can access any element in the document from any reference. But that is not necessary for the form objects.
Chinmay PatelChief Technology NinjaCommented:
Hi Zvonko,

my mistake.... I accept....
;) tnx

Regards,
Chinmay
morpheus3gAuthor Commented:
i've noticed that it isnt working with mozilla.. i need something cross-platform...

ZvonkoSystems architectCommented:
Check this:

<BODY onkeypress="SubmitMyForm(event)">


<SCRIPT>
function SubmitMyForm(e){
  var keyCode=(e.keyCode)?e.keyCode:e.which;
  if (keyCode==13){
      document.forms[0].submit();
   }
}
</SCRIPT>

morpheus3gAuthor Commented:
not working with firefox
ZvonkoSystems architectCommented:
This worked for me in FF1.0.3:

<BODY onkeypress="SubmitMyForm(event)">
<form>
</form>
</body>
<SCRIPT>
function SubmitMyForm(e){
  var keyCode=(e.keyCode)?e.keyCode:e.which;
  if (keyCode==13){
      alert("Submitting...")
      document.forms[0].submit();
   }
}
</SCRIPT>


Do you have a <form> tag on your page?

morpheus3gAuthor Commented:
that won't for me.
ff 1.04

:(
ZvonkoSystems architectCommented:
You have elsewhere a typo.
Show your page html source. Or is your page online?
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.