jQuery Detect ENTER key Press

jQuery Detect ENTER key Press

To detect pressing Enter on keyboard using jQuery; In this tutorial, you will learn how to detect pressing Enter on keyboard using jQuery keypress() event method.

How to detect pressing Enter on keyboard using jQuery

To determine whether the user has pressed the “Enter” key on web app pages to submit a form, detect the event on textboxes, input boxes, and clicks using the jQuery keypress() event. Here are examples of using the jQuery keypress() event:

  • jQuery keypress() Event Method
  • Syntax of jQuery keypress() Event Method
  • Parameters of jQuery keypress() Event Method
  • Examples of jQuery Detect ENTER key Press

jQuery keypress() Event Method

The jQuery keypress() event occurs uniquely when a keyboard button is pressed. This keypress event bears resemblance to the keydown event. When the keypress() event occurs, associated functions are executed through the keypress() method.

Syntax of jQuery keypress() Event Method

$(selector).keypress() 

This triggers the keypress event for the selected elements.

$(selector).keypress(function)  

Parameters of jQuery keypress() Event Method

ParameterDescription
FunctionIt is an optional parameter. It is executed itself when the keypress event is triggered.

Examples of jQuery Detect ENTER key Press

Let’s see an example1 with demonstrate jQuery keypress(); as shown below:
<!DOCTYPE html>  
<html>  
<head>  
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>  
i = 0;  
$(document).ready(function(){  
    $("input").keypress(function(){  
        $("span").text (i += 1);  
    });  
});  
</script>  
</head>  
<body>  
Enter something: <input type="text">  
<p>Keypresses val count: <span>0</span></p>  
</body>  
</html>  
Enter something:

Keypresses val count: 0

Let’s see an example 2 with demonstrate jQuery keypress(); as shown below:

To check whether the user has pressed the ENTER key on the webpage or on an input element, you can tie a keyup or keydown event to that element or document object.

If the code of the key pressed code is 13 then the “ENTER” key was pressed; Otherwise some other key was pressed.

<html>
<head>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
    <h1>Write something here & press enter</h1>
    <label>TextBox Area: </label>
    <input id="someTextBox" type="text" size="100" />
    <script type="text/javascript">
        //Bind keypress event to textbox
        $('#someTextBox').keypress(function(event){
            var keycode = (event.keyCode ? event.keyCode : event.which);
            if(keycode == '13'){
                alert('You pressed a "enter" key in textbox'); 
            }
            event.stopPropagation();
        });
    </script>
</body>
</html>

Write something here & press enter

Recommended jQuery Tutorials

  1. jquery keyup event example
  2. Jquery Click() Event Method with E.g.
  3. Event jQuery. Blur By Example
  4. jQuery form submit event with example
  5. keydown function jQuery
  6. List of jQuery Events Handling Methods with examples
  7. Jquery Selector by .class | name | #id | Elements
  8. How to Get the Current Page URL in jQuery
  9. jQuery Ajax Get() Method Example
  10. get radio button checked value jquery by id, name, class
  11. jQuery Set & Get innerWidth & innerHeight Of Html Elements
  12. jQuery Get Data Text, Id, Attribute Value By Example
  13. Set data attribute value jquery
  14. select multiple class in jquery
  15. How to Remove Attribute Of Html Elements In jQuery
  16. How to Checked Unchecked Checkbox Using jQuery
  17. jQuery removeClass & addClass On Button Click By E.g.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *