LearnFast.Ninja

jQuery keypress enter on key code number execute function

Write the following example and then when the user press enter key the browser pop alert box. See the result at your browser (Press the enter key…). Note: We use keydown to handle arrow key, control, and alt key.

Step 1: Write the following example and then when the user press enter key the browser pop alert box. 

<html>

<head>

    <title>Enter key</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {

            $(document).keydown(function (event) { // or keypress

                console.log(event.keyCode);

                if (event.keyCode == '13') {

                    alert('Enter key is pressed (keyCode: ' + event.keyCode + ')');

                }

            });

        });

    </script>

</head>

<body>

</body>

</html>

 
Result: (Press the enter key…) 


Note: We use keydown to handle arrow key, control, and alt key

Tags

jquery

key keycode