gopigoppu
gopigoppu

Reputation: 219

Why trigger click not working in javascript

i'm trying to put simple login form with enter key. The login functionality placed in login button click. I just trying to trigger it on keypress of enter button. But its not working. What is my problem?

  $("#userPassword").bind('keypress', function(e) {
       var code = (e.keyCode ? e.keyCode : e.which);
       console.log("trigger outside "+code);
        if(code == 13) 
        {   
            console.log("trigger insided");
            $( "#loginBtn" ).trigger( "click" );

        }
     });

Upvotes: 1

Views: 400

Answers (2)

HCJ
HCJ

Reputation: 527

How have you defined or handled the click event on the login button? If this isn't working, then I think you have not bind any function to the click handler and login is working some other way.

From the docs

$( "#foo" ).on( "click", function() {
  alert( $( this ).text() );
});
$( "#foo" ).trigger( "click" );

Upvotes: 1

Denis Shulepov
Denis Shulepov

Reputation: 381

If you are trying to make your code run when user presses "Enter" in any of the field in your login form then you are doing it wrong. The proper way is to wrap your login/password fields and submit button into a form and write a form on submit handler.

Upvotes: 1

Related Questions