Cross Browser "Form Auto Submit on Enter"

M

mattgarvin

Hello,
I need a piece of javascript that will allow a form to be submitted
when Enter is pressed for IE6 and 7, Firefox 2, and recent versions of
Safari. It is for a login page that has two textboxes: a "Login" and
a "Password". The "Login Button" is actually a standard image with an
onClick event handler to submit the form, because we use a mouseover
effect. (That is, it is not INPUT TYPE="IMAGE" but rather an IMAGE
tag with onclick="document.forms[0].submit()" added.)

I found some code that works in IE, which is below, but it only works
in IE. I am sure there is a simple cross-browser fragment of
Javascript that will do the trick, so please point me in the right
direction.

Here is the code I am using that only works for IE.
....
<head>
<script type="text/javascript">

function handle_keypress(e)
{
if(window.event.keyCode==13)
{
delayed_submit_form()
}
else
return false;
}

</script>
</head>

<body onKeyPress="handle_keypress(event)">
....

Thanks for any help you can give me,
Matt Garvin
 
A

Adambrz

Hello,
I need a piece of javascript that will allow a form to be submitted
when Enter is pressed for IE6 and 7, Firefox 2, and recent versions of
Safari. It is for a login page that has two textboxes: a "Login" and
a "Password". The "Login Button" is actually a standard image with an
onClick event handler to submit the form, because we use a mouseover
effect. (That is, it is not INPUT TYPE="IMAGE" but rather an IMAGE
tag with onclick="document.forms[0].submit()" added.)

I found some code that works in IE, which is below, but it only works
in IE. I am sure there is a simple cross-browser fragment of
Javascript that will do the trick, so please point me in the right
direction.

Here is the code I am using that only works for IE.
...
<head>
<script type="text/javascript">

function handle_keypress(e)
{
if(window.event.keyCode==13)
{
delayed_submit_form()
}
else
return false;
}

</script>
</head>

<body onKeyPress="handle_keypress(event)">
...

Thanks for any help you can give me,
Matt Garvin

I am not so sure about Safari but for ie and firefox I know that the
following works:
<html>
<script language="javascript">
function myKeyPressed(e){
e = e || window.event;
var unicode=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if (unicode == 13){
document.formname.submit();
}
}
document.onkeypress = myKeyPressed;
</script>
<body>
<form name="formname" action="#" method="get">
<input name="test" type="text" value="test" size="12" />
</form>
</body>
</html>
 
M

mattgarvin

Thank you for the code, Adam.

I tested it, and indeed it did work with IE and FF. Like you, I have
yet to test it with Safari, but it is covering the two biggies now.

Thank you,
Matt Garvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top