I’m writing some unit test in my web apps and I want to automatically trigger an Enter key pressed event from my page after for example a download to text file button was clicked. How can i do this in jQuery or Javascript?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Create a variable that tells us if the user has clicked your button.
Add an event listener to your button, so that when the user clicks on it, the
clickedvariable will becometrue.Add a
keypressevent listener:Inside that event listener, check if the user clicked previously
If (s)he did, check the pressed key. Enter’s key code is 13.
If the pressed key was enter, use
Otherwise, once the user clicked your button and pressed enter, it wouldn’t be necessary to click the button again.
After that run your code. For example, this will call function
fafter 2 seconds.Live demo