User Id :    Password :      New Member   Forgot Password  
Making a button default clickable when enter key is pressed
Description Java-Script function to make a button default clickable on enter key press.   No. of Views     3452
  Rating     0
Author Sumit Gupta   Posted On     11 Jul 2011
Tags ASP.NET,Java Script    

Sample Code   Download Code

Many 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
                        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
                                    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...

About Author

About Author I am Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
Country India
Company 3 Pillar Global Pvt. Ltd.
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Posted By Annonymous on 02 Sep 2011 at 10:26 PM
Begun, the great inetrent education has.
Write your comment here.
Verification Code