views:

316

answers:

2

Hi,

This is driving me nuts. Its a tough one to explain but Ill have a go.

I have one input text field on the front page of my site. I have coded a keydown event observer which checks the keyCode and if its ENTER (or equiv), itll check the input value (email). If the email is valid and unique in the DB itll submit the form. Basic stuff, or so you would think.

If I type my email address in the field and hit enter, it works fine in all browsers. However, if I type the first couple of letters, and then use the arrow keys to select the email from the history dropdown box (hope you know what I mean here), and then press enter the result is different. The value of the form field is being captured as just the couple of letters I typed, and therefore the validation is failing. It seems that when I press the enter key to "select" the email from the history dropdown, the browser is interrupting that as if I was typing.

In Chrome and Safari it works as it should. As it should means that when you press enter to "select" the email from the history dropdown, all it does is puts that email address into the text box. Only on the second ENTER key press does it then trigger the event observer, and the email is validated.

Hope somebody can shed some light on why this is happening... My gut feeling is its a browser thing and will be something I cant fix.

Thanks Lee

EDIT: To add clarification to my question let me add that Im using the "keydown" event to capture the moment when the enter key is pressed. I have tried the "keyup" event and this solved my problem above, but then I cant seem to stop the form submitting by itself. The "keyup" event triggers AFTER the default behaviour, therefore its not the right choice for this.

FURTHER EDIT:

Thank you again, and btw, your English is excellent (in response to your comment about bad English).

I have changed my event handler from this:

$("emailInputBox").observe("keydown", function(event) {
    return submitViaEnter(event, submitSignupFormOne);
});

to this:

$("emailInputBox").observe("keydown", function(event) {
    setTimeout(submitViaEnter.curry(event, submitSignupFormOne),0);
});

submitViaEnter:

function submitViaEnter(event, callback) {
var code = event.keyCode;
if (code == Event.KEY_RETURN) {
    event.stop();
    return callback(event);
}
return true;
}

Seems to work but the problem now is that the browser is permitted to carry out the default action before running the submitViaEnter function which means the form is being submitted when I hit ENTER.

+3  A: 

Answer to the original question

Yeah, it's a Gecko bug (not Mac-specific though).

The last part of this comment contains the description of the work-around: use the time-out.

[edit] since you asked for the clarification of the bug

When you press Enter and the auto-complete is active, Firefox (erroneously) first fires the page's key handler, then the browser's internal key handler that closes the autocomplete popup and updates the text area value, while it arguably should just fire it at the autocomplete popup and only let the page know the textbox value changed.

This means that when your key handler is called, the autocomplete's handler hasn't run yet -- the autocomplete popup is still open and the textbox value is like it was just before the auto-completion happened.

When you add a setTimeout call to your key handler you're saying to the browser "hey, run this function right after you finished doing stuff already in your P1 to-do list". So the autocomplete's handler runs, since it's already in the to-do list, then the code you put on a time-out runs -- when the autocomplete popup is already closed and the textbox's value updated.

[edit] answering the question in "Further edit"

Right. You need to cancel the default action in the event handler, not in the timeout, if you want it to work:

function onKeyPress(ev) {
  if (... enter pressed ...) {
    setTimeout(function() {
      ... check the new textbox value after letting autocomplete work ...
    }, 0);
    // but cancel the default behavior (submitting the form) directly in the event listener
    ev.preventDefault();
    return false;
  }
}

If you still wanted to submit the form on Enter, it would be a more interesting exercise, but it doesn't seem you do.

Nickolay
Thanks!! Ive worked around it by disabling the autocomplete functionality for the form but I dont like it.Do you know what "the time-out" is, and if so, can you elaborate please?Thanks again.
LeeRM
@LeeRM: in the keydown handler call setTimeout(handler2, 0) and move your validation code into |function handler2() { }|.
Nickolay
oh I see. Im sorry if Im being thick here. Just when I think Im getting good at this stuff...What is the outcome of this? I know it fixes the problem Im having but how? I like to understand whats going on. Thank you for your help!
LeeRM
@LeeRM: edited the answer.
Nickolay
thank you for taking the time to do that for me! I really appreciate it. So a timeout of zero is ok?
LeeRM
@LeeRM: it should be ok -- I explained the theory -- but I didn't actually test it. Increasing the timeout value (if a zero-timeout didn't work) would be a hack -- since it makes the results depend on the timing of events. So if the zero-timeout doesn't work for some reason, I'd be surprised and would need to investigate it closer.
Nickolay
Removed this comment and amended original post... thanks.
LeeRM
thanks again. I do actually want to submit the form on enter also. I need enter to do the validation, and if ok, submit the form. Ill have a play with the new info youve supplied. Cheers. Lee
LeeRM
removed and added as answer
LeeRM
+1  A: 

ok sorted it. Thanks so much for your help. It was the curry function that I was missing before. I was trying to work on the event inside the scope of the setTimeout function.

This works below. The submitViaEnter is called from the eventobserver and responds to the keyDown event:

function submitViaEnter(event, callback) {
var code = event.keyCode;
if (code == Event.KEY_RETURN) {
    event.stop();
    setTimeout(callback.curry(event),0);        
    // return callback(event);
    // return false;    
}
return true;
}

Stopping the default action inside the eventObserver meant that no characters could be typed. So I stuck inside the if ENTER key clause.

LeeRM