Forum Moderators: open

Message Too Old, No Replies

submit a form in netscape on enter key

         

manjumurthy

1:37 pm on May 13, 2002 (gmt 0)



Hi all,
I have a html page with login id & password. I want to submit the form on pressing the return key. This works well in I.E but not in netscape. Can anybody giv me the solution....
Thanks
manjumurthy

moonbiter

2:42 pm on May 13, 2002 (gmt 0)

10+ Year Member



Theoretically, this should work by default if your form has just one input type="text", any number of other input elements, and no textarea.

In reality, Netscape Navigator seems to treat the input type="password" as a second input type="text" area, so this default doesn't work. You need to use javascript to do this. Something like:

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
NS4 = (document.layers) ? true : false;

function checkEnter(event)
{
var code = 0;

if (NS4) {
code = event.which;
}
else {
code = event.keyCode;
}
if (code==13) {
document.monkeyform.submit();
}
}
</script>
</head>

<body>
<form action="" name="monkeyform" id="monkeyform">
<input type="text" name="monkeyText" id="monkeyText" onkeypress="checkEnter(event)" />
<input type="password" name="monkeyPass" id="monkeyPass" onkeypress="checkEnter(event)" />
<input type="submit" name="monkeySubmit" id="monkeySubmit" />
</form>
</body>
</html>

Not incredibly elegant, but it seems to work. A javascript guru could probably give you a better solution.