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

About Author

About Author I am cool and fun loving guy and enjoy my work. I love music and play table-tennis.... Sumit Gupta
No Photo
 
Country India
Company BrickRed Technologies Pvt. Ltd.
Home Page http://www.facebook.com/sumitgupta1225

Rate this article

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

Comments

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