469,319 Members | 2,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

Cross Browser "Form Auto Submit on Enter"

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

Feb 8 '07 #1
2 11544
On Feb 8, 12:58 pm, mattgar...@gmail.com wrote:
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>

Feb 8 '07 #2
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

Feb 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Questman | last post: by
1 post views Thread by laredotornado | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.