1
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required="" autofocus="" />
<input type="password" class="form-control" id="password" name="pwd" placeholder="Password" required="" autofocus="" />
<input class="btn btn-lg btn-primary btn-block button marginT20"  name="login" value="Login" type="Submit" onclick="login(document.getElementById('username').value,document.getElementById('password').value)" >   

<script>  
functionlogin(username, password){
  $.ajax({
    method: "POST",
    url: "/login",
    data: {
      username: username,
      pwd: password
    }
  }).done(function(msg1){
    if(msg1=='success'){
      window.location='/home';
    }else{
      varusername='username';varpassword='password';eraseCookie(username,
      password);toastr.warning(msg1);
    }
  });
}  
</script>

My code is working when someone presses the login button, but I want the username and password filled in if the user presses enter. Then the form is submitted with the existing ajax code.

ozil
  • 6,001
  • 8
  • 28
  • 50
Sumit Aggarwal
  • 741
  • 2
  • 10
  • 27

2 Answers2

1

Do following steps :

  1. Wrap your HTML code around a form
  2. In your form use onsubmit attribute to call the same method as on the click of submit button
Ajay2707
  • 5,345
  • 6
  • 34
  • 52
Himanshu Tyagi
  • 5,025
  • 1
  • 20
  • 42
1

Try this

 $('#username , #password').keypress(function (e) {
  var key = e.which;
  if(key == 13)  // the enter key code
   {
        var username =$('#username').val();
        var password =$('#password').val()
        login(username, password);
   }
});
shilpi Singh
  • 402
  • 1
  • 5
  • 15