Pressing Enter activates Button

Hi all, have a question and can’t get it to work. I know there are several posts out there, but can someone help in how I can get pressing ‘Enter’ to activate my Login button? I don’t know where to add it and I am not a coder… Thanks so much! My script is as follows:

import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;
import wixWindow from ‘wix-window’ ;

$w.onReady( function (){
$w( ‘#loginNow’ ).onClick( function (){
let email = $w( ‘#emailLogin’ ).value;
let password = $w( ‘#passwordLogin’ ).value;
wixUsers.login(email,password)
.then(()=>{
console.log( “User is logged in” );
wixLocation.to( ‘/account/my-account’ );
})
. catch ( (err) => {
console.log(err);
$w( “#errorMessage” ).expand();
} );
})
})
$w.onReady( function () {
$w( “#forgotPassword” ).onClick( (event) => {
//wixWindow.lightbox.close()
wixLocation.to( ‘/test’ )
.then( ( ) => {
//
} )
})
})

@stellie_98 Creating an onKeyPress event for the password field would be the way to do that, and put the login button code in a separate function that you can call from other functions. You need only one page onReady.

$w.onReady(function () {
   $w("#passwordLogin").onKeyPress((event) => {
      if (event.key === 'Enter'){
         Login();
      }
   })
   $w('#loginNow').onClick(function (){ 
      Login();
    })
    // other functions
});

export function Login(){
   let email = $w('#emailLogin').value;   
   let password = $w('#passwordLogin').value;          	     	
   wixUsers.login(email,password)  
   // ... rest of code
}

@tony-brunsman Many thanks!! It works perfectly now. Appreciated.

By the way is there a way to get the email/password error to disappear after a few seconds (currently when you type in a new one and it’s still incorrect, it’s difficult to see it captured it). Thanks again!

@stellie_98 Looking at this, I realized that I didn’t put the enter key test in the original post. It has been corrected.

Does that change anything with the email/password error?

@tony-brunsman actually works the same. Strange that it already recognized the enter key previously. But changed it as indeed should be better.

It doesn’t change the email/password error… Best would be if it disappears as soon as you start typing again, so you can see (when clicking or pressing enter) if the new combination you typed in is also incorrect.