Making a button default clickable when enter key is pressedMany times we want that on pressing enter key the submit button should be automatic click. For example on login page after typing password generally users press enter key and want that submit button will call but nothing happen.
So for that purpose I am giving a little java script code that can do this task.
//the purpose of this function is to allow the enter key to point to the correct button to click.
function doClick(buttonName, e) {
var key;
if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13) { //Get the button the user wants to have clicked
var btn = document.getElementById(buttonName);
if (btn != null) { //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}
In the page load event add the following line to set this function call on key press event.
txtPassword.Attributes.Add("onKeyPress", "doClick('" + btnSignin.ClientID + "',event)");
Happy coding...