Using onKeyDown to check for form submission is bad, mmk?

Virgin America ScreencapI’m a big fan of AJAX submission of forms. Recently I’ve gotten lazy and omitted the FORM tag for single (and dual) input interfaces, instead checking for enter-keypress form submission using the onKeyDown event on my form elements.

This lazyness is now biting me in the butt when using Virgin America, Washington Mutual, and many other commercial sites. When the user is entering text into a text input element, many browsers present a pull-down menu of previous inputs for similar fields.

Using latest FireFox on OS X, the enter event on the browser’s drop down menu is propagated to the HTML input element. Although I pressed enter in the context of a browser interface component, the event is propagated to the page element, where it triggers form submission.

Lesson learned: Always use onsubmit on your surrounding form element. Don’t have a form element? You might be unpleasantly surprised that IE6 will create one for you, using all input elements on the page as form elements and the page’s URL as the POST destination. Remove ambiguity, remove fail.

Advertisement

~ by philippp on March 4, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.