The moose likes HTML, CSS and JavaScript and the fly likes Problem with submit......... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Problem with submit........." Watch "Problem with submit........." New topic
Author

Problem with submit.........

Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Is there a Java script event handler that get fired, when a user presses the return key (enter) while using my JSP screen. Because,
i just seem to get my error page (JSP), if the user presses enter key, instead of the
submit button(or <input type="button">
All ideas will be appreciated !!!
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50673

Well, you can certainly capture the key event but I don't think that's the right thing to do.
The form itself has an on_submit handler (minus the _) that you can use to validate that the values to be submitted are "legal".
First thing I'd do is to debug the servlet (or whatever backend component you are using) to find out why it is blowing up when you submit the form via the return key vs. your button. Then you can either deal with the situation which causes the error in an intelligent fashion either on the server side or on the client side (or both).
hth,
bear


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
christoph weingarten
Ranch Hand

Joined: Oct 10, 2002
Posts: 39
hi gabriel,
there is a keyCode-attribute (window.event.keyCode) you could use. but as far, as i know it will only work in ie. netscape-browsers can't handle it.
maybe you can solve your problem by setting the focus-attribute manually:

check where the focus is, when you hit enter.

christoph
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Originally posted by christoph weingarten:
there is a keyCode-attribute (window.event.keyCode) you could use. but as far, as i know it will only work in ie. netscape-browsers can't handle it.
christoph

Netscape CAN handle it if you use the right code for netscape.
post the code for the submitting of your form.
Eric
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 411
This code handles IE4+,NS4, NS6+.
It will submit a given form if enter pressed.
If you want to submit a form only if user pressed enter on the form element, you need to get a form usling window.event.srcElement.form for IE , and (I think, I remember correctly ) e.target.form for NS, into an if statement.
function NSkeypress(e)
{
if (e.which == 13) {
top.document.MyForm.submit();
}
return true;
}
function IEkeypress()
{
if (window.event.keyCode == 13) {
top.document.MyForm.submit();
}
return true;
}

//define event handlers
if (document.getElementById&&!document.all) {
// NS6 - not sure,that you need to use a captureEvents
document.captureEvents(Event.KEYPRESS);
document.onkeypress= NSkeypress;
}
if (document.layers) {
document.captureEvents(Event.KEYPRESS);
document.onkeypress= NSkeypress;
}
if (document.all) {
document.onkeypress=IEkeypress;
}
Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Hello, the IE version works fine , but what is the actual value of the parameter, i could not get this in my Javascript reference.
i.e. value of e and e.which in the function below
function NSkeypress(e)
{
if (e.which == 13) {
top.document.MyForm.submit();
}
return true;
}
Cheers.
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 411
It is an event object, it would be passed by browser, when it calles your event handler
Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Thanks very much Yuriy.
 
 
subject: Problem with submit.........
 
Threads others viewed
Form submit
how to make the button default when press enter
Problem with submit
How to submit multiple records in one trip
HTML +Javascript submit query??
developer file tools