Closed Bug 85894 Opened 24 years ago Closed 24 years ago

onkeypress event handler envoked too late when assigned through script

Categories

(Core :: DOM: Events, defect)

x86
Windows 95
defect
Not set
normal

Tracking

()

VERIFIED DUPLICATE of bug 54035

People

(Reporter: gwagner, Assigned: joki)

Details

Two examples follow based on the following keypress handler and form code: function positiveIntInputOnly(e) { // returns true if 0-9 or BS hit or browser isn't IE or NS, otherwise false alert('keypress handler'); var nKeyCode = -1; if (e && e.which) nKeyCode = e.which; // NS4 & NS6 else if (window.event && window.event.keyCode) nKeyCode = window.event.keyCode; // IE return (nKeyCode > -1 ? ((nKeyCode > 47 && nKeyCode < 58) || nKeyCode == 8) : true); } // positiveIntInputOnly() <form name="myForm"> <input type="text" name="myInput"> </form> If I statically assign the event handler to the input: <input type="text" name="myInput" onkeypress="positiveIntInputOnly();"> everything works as expected, all characters other than 0-9 and BckSpc are rejected. As well, the alert() appears BEFORE the character is added to input box (since the character can't be added until the event handler returns either true or false). However, if I programmatically assign the event handler: <body onload="document.myForm.myInput.onkeypress=positiveIntInputOnly;"> the event handler gets attached to the input element (since the alert() triggers), but what seems to happen is that the alert() appears AFTER the character is added to the input. In other words, the keypress adds the character to the input, THEN triggers the event handler, at which point it doesn't matter what the event handler returns, the character is in the input. This may be related to what is going on in bug 81739.
Interesting... cofirming
Status: UNCONFIRMED → NEW
Ever confirmed: true
*** This bug has been marked as a duplicate of 54035 ***
Status: NEW → RESOLVED
Closed: 24 years ago
Resolution: --- → DUPLICATE
verifying
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.