Fire a button click when ENTER is pressed in a textbox
How many times have you visited a website, typed something like your email address into a textbox (perhaps
for a newsletter signup) and pressed the ENTER key to submit the form? Maybe it's out of habit, but the
expectation is pressing ENTER will fire some click event to submit your data. If it doesn't fire a submit,
you're stuck having to manually click a "submit" button via the mouse. Very annoying. Avoid making that
mistake by firing a button click from any textbox on your form with some simple JavaScript code.
Clearing dropdowns
//Fire the button click of our "Signup" button when a user presses ENTER
//in the email address text box when signing up for our newsletter.
txtEmailAddress.Attributes.Add("onkeydown",
"javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){document.getElementById('" + btnSignup.ClientID + "').click();return false;}else return true;");
The majority of people on the web really aren't all that tech-savvy. In fact, I would go a step further
and say their down-right lazy! They're not going to want to lift their fingers from the keyboard to grab
hold of their mouse and click a button. That's like... what... 2 extra steps? Why frustrate people
whom you're trying to win over? The fact is, as a webmaster, you'll loose out on potential customers
by not putting emphasis on the user experience. Users expect things to be easy, so you might as well
accommodate them or risk them taking their business elsewhere.
More information
Drop-In Code