Javatpoint Logo
Javatpoint Logo

jQuery keypress() method

The keypress() method in jQuery is used to trigger the keypress event or attaches a function to execute when a key of the keyboard is press down, i.e., when a keypress event occurs. This method can be used to check whether any keyboard key is pressed or not.

This method doesn't consider the keyboard buttons like Ctrl, Alt, Shift, Esc. We can use the keydown() method to check these keys. The keypress event is similar to the keydown event.

Syntax

The keypress() method is used with or without using any argument. The syntaxes of using the keypress() method are given as follows -

Trigger the keypress event

Attach a function to execute when a keypress event occurs

The function mentioned in the above syntax is the optional parameter. It specifies the function that executes when keypress event triggers.

Let's understand the working of the keypress() method using some illustrations.

Example1

In this example, we are using the keypress() method to count how many times a user gives input in the given textfield. Here, there is a textfield that requires some input. When a user writes something in the textfield, the input will be count every time.

Here, we are using the optional parameter of the keypress() method. So, the method will trigger the handler on pressing a key. It will work on all keyboard keys except some special keys such as Shift, Delete, Esc, etc.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery keypress() method

After writing something in the given textfield, the output will be -

jQuery keypress() method

Example2

In this example, we are using the keypress() event with the handler parameter to detect a keypress event. The method will trigger the handler once a key is pressed. When user will click a key from the keyboard, an alert box will be displayed showing a message.

Although the method will trigger the handler for all keys except some special or non-printing keys such as Ctrl, Shift, Esc, etc.

If we use the keydown() method instead of using the keypress() method, the handler will trigger all keys of the keyboard, including special or non-printing keys.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery keypress() method

After pressing a key from the keyboard, the output will be -

jQuery keypress() method

Example3

It is another example of using the keypress() method. It is similar to the above example, except that here the alert box will display which key is pressed from the keyboard.

In this example, we are using the keypress() method to detect which key is pressed from the keyboard. The method will trigger the handler for all keyboard keys except some special or non-printable keys.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery keypress() method

After pressing a key from the keyboard, the output will be -

jQuery keypress() method

Similar to the above screenshot, the alert box will be displayed for every key except for some special keys.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA