jQuery keypress() Method Last Updated : 20 Dec, 2023 Comments Improve Suggest changes Like Article Like Report jQuery keypress() method triggers the keypress event whenever the browser registers a keyboard input. So, Using the keypress() method it can be detected if any key is pressed or not. NOTE: The keypress event will not trigger for all keys like non-printing characters (e.g. ALT, CTRL, SHIFT, ESC). The keydown() method can be used to check these keys. Syntax:$(selector).keypress(function) Example 1: The below code is used to check if a key is pressed anywhere on the page or not using the keypress() method of jQuery. HTML <!DOCTYPE html> <html> <head> <title>Jquery | Keypress() </title> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script> $(document).ready(()=>{ $(document). keypress(function (event) { alert('You pressed a key'); }); }); </script> </head> <body> <h1> Try pressing any printable character from the keyboard </h1> </body> </html> Output: Example 2: The below code example will show you the name of the key pressed by you in the alert message using keypress() method. HTML <!DOCTYPE html> <html> <head> <title>Jquery | Keypress() </title> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script> $(document).ready(()=>{ $(document). keypress(function (event) { let key = (event.keyCode ? event.keyCode : event.which); let character = String. fromCharCode(key) alert('You pressed key : ' + character); }); }); </script> </head> <body> <h1> Try pressing any printable character from the keyboard </h1> </body> </html> Output: Example 3: The below example will show an alert message if you click the enter key inside the input box. HTML <!DOCTYPE html> <html> <head> <title>Jquery | Keypress() </title> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script> $(document).ready(()=>{ $('#textbox'). keypress(function (event) { let keycode = (event.keyCode ? event.keyCode : event.which); if (keycode == '13') { alert('You pressed "enter" key in textbox'); } event.stopPropagation(); }); }); </script> </head> <body> <center> <br><br> <h1> Press " Enter key " inside the textbox </h1> <input id="textbox" type="text" size="50" /> </center> </body> </html> Output: Comment More infoAdvertise with us Next Article jQuery keypress() Method I ihritik Follow Improve Article Tags : JavaScript Web Technologies JQuery jQuery-Methods Similar Reads jQuery bind() Method The jQuery bind() method is used to attach one or more event handlers for selected elements. This method specifies a function to run when an event occurs. Syntax$(selector).bind(event, data, function);ParametersIt accepts three parameters that are described below: event: This is an event type that 2 min read jQuery blur() Method jQuery blur() is an inbuilt method that is used to remove focus from the selected element. This method starts the blur event or it can be attached a function to run when a blur event occurs. Syntax:$(selector).blur(function)Parameters: It accepts an optional parameter "function". jQuery examples t 1 min read jQuery change() Method The jQuery change() method is used to detect changes in the value of form elements like <input>, <select>, or <textarea>. It triggers a specified function when a user modifies the input or selects a different option.Syntax$(selector).change(function)Parameters : It accepts an optio 2 min read jQuery click() Method jQuery click() is an inbuilt method that starts the click event or attaches a function to run when a click event occurs. Syntax:$(selector).click(function);Parameters: It accepts an optional parameter "function" which is used to run when a click event occurs. jQuery examples to show the working of 2 min read jQuery dblclick() Method jQuery dblclick() is an inbuilt method that is used to trigger the double-click event to occur. This method occurs when the selected element will be double-clicked. Syntax:$(selector).dblclick(args);Here "selector" is the selected element. Parameters: It accepts an optional parameter "args" which 2 min read jQuery event.isDefaultPrevented() Method jQuery isDefaultPrevented() Method is an inbuilt method that checks whether the preventDefault() method was called for the event. This method returns a boolean value. It returns True if preventDefault() is called on the event, False otherwise. Syntax:event.isDefaultPrevented()Parameters: It accepts 2 min read jQuery event.isImmediatePropagationStopped() Method jQuery isImmediatePropagationStopped() Method is used to check whether this method was called for the event or not. If it was called then it will be "true" or else "false". Syntax:$(selector).isImmediatePropagationStopped()Parameters: This method accepts only one parameter as a selector which is us 2 min read jQuery event.isPropagationStopped() Method jQuery event.isPropagationStopped() Method is used to check whether the object event.stopPropagation() is called or not. If event.stopPropagation() is called then it returns true otherwise returns false. Syntax:event.isPropagationStopped()Parameters: It contains a single parameter event which is ma 1 min read jQuery event.preventDefault() Method The jQuery preventDefault() Method is used to stop the default action of the selected element to occur. It is also used to check whether preventDefault() method is called for the selected element or not. Syntax: event.preventDefault()Parameter: It does not accept any parameter. Example 1: This examp 2 min read jQuery event.stopImmediatePropagation() Method The jQuery event.stopImmediatePropagation() is an inbuilt method in jQuery used to stop the rest of the event handlers from being executed for the selected element. Syntax: event.stopImmediatePropagation() Parameter: No parameter is required. Example 1: Here, only first pop box will appear after thi 2 min read Like