Closed Bug 81739 Opened 23 years ago Closed 20 years ago

onKeyPress event handler can't cancel form.submit triggered by pressing Enter in text input [form sub]

Categories

(Core :: DOM: UI Events & Focus Handling, defect)

x86
Windows NT
defect
Not set
normal

Tracking

()

RESOLVED WORKSFORME
mozilla1.2beta

People

(Reporter: david.manz, Assigned: saari)

References

Details

(Keywords: testcase, Whiteboard: [eapp])

Attachments

(2 files)

If there is a single input type=text on a page, pressing enter in the input will
cause the form to be submitted.  In both IE and Netscape 4, this can be prevented
by returning false from an onKeyPress event handler.  However, this does not work
in Mozilla.
Attached file simple test case
Keywords: testcase
Status: UNCONFIRMED → NEW
Ever confirmed: true
onkeypress handler is handled by the form before it reaches the text box 
check... looks like the form captures the return key. Even if I add this handler 
 to form itself the enter key submits it before reaching the handler. 
 The only workabout I could come up with is this:

if(keycode == 13) {
    if(e.stopPropagation)
     e.stopPropagation();
    return false; }
}

if(document.addEventListener)
  document.addEventListener("keypress",cancelSubmit,1); 

Basicly capture the event at document level and not let it go any further. This 
works for not submitting the form, but if there is a text area you wont be able 
to go to the next line.
reassigning
Assignee: rods → pollmann
I could be wrong, but I think this could be generalized. If you return false 
from the onKeyPress handler, the key press should not be propagated regardless 
of which key was pressed. In theory, you should be able to prevent the user 
from entering any given keypress, be it <Return> or the 'X' key, or whatever, 
into a given control.

In Mozilla, the keypress, and hence the character, seems to be reaching the 
control before the onKeyPress handler is even called, thus returning false has 
no effect.
Bulk reassigning form bugs to Alex
Assignee: pollmann → alexsavulov
Summary: onKeyPress event handler can't cancel form.submit triggered by pressing Enter in text input → onKeyPress event handler can't cancel form.submit triggered by pressing Enter in text input [form sub]
We encountered similar problem but I think it is related so I will add a 
comment here instead of opening a new but.

When we set an event handler, the handler is triggered OK when the Enter key is 
pressed but the form is submitted prematurely before the handler is completed 
(which means that returning 'false' from the handler will not suppress the form 
posting). The same code worked very well for us on NS 4.7 .

See attachment 'Demonstrates how the form is posted before the event handler is 
completed' for instructions how to reproduce it.
reassigning
Component: HTML Form Controls → Event Handling
reassigning (forgot to check the radio first time)
Assignee: alexsavulov → joki
QA Contact: vladimire → madhur
dup of another soon to be in 0.9.9
Target Milestone: --- → mozilla0.9.9
Depends on: 124990
nsbeta1+ per ADT triage
Keywords: nsbeta1+
124990 and its associated bugs is not quite ready for checkin for 0.9.9. 
Maintaining high priority and moving to 1.0 for completion and further testing.
Target Milestone: mozilla0.9.9 → mozilla1.0
-> eapp per oracle
Blocks: 125136
Whiteboard: [eapp]
QA Contact: madhur → rakeshmishra
this also happens on Windows XP as well as Windows ME
Looks like we have missed the train for 1.0.1. Can we retarget this for 1.2?
the work this depends upon will be landing soon
Target Milestone: mozilla1.0 → mozilla1.2beta
Blocks: 157568
QA Contact: rakeshmishra → trix
.
Assignee: joki → saari
QA Contact: trix → ian
This worksforme in a current build.  Also verified by code inspection that we
check whether the event was cancelled before triggering submit-on-enter.
Status: NEW → RESOLVED
Closed: 20 years ago
Resolution: --- → WORKSFORME
Component: Event Handling → User events and focus handling
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: