Link to home
Start Free TrialLog in
Avatar of morpheus3g
morpheus3g

asked on

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.

ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(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();

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
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.
Hi Zvonko,

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

Regards,
Chinmay
Avatar of morpheus3g
morpheus3g

ASKER

i've noticed that it isnt working with mozilla.. i need something cross-platform...

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>

not working with firefox
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?

that won't for me.
ff 1.04

:(
You have elsewhere a typo.
Show your page html source. Or is your page online?