Knowledge Walls
John Peter
Pune, Maharashtra, India
How to call javascript function on enter keypress in textbox with Example
60452 Views
Example
<html>
    <head>
        <script type="text/javascript">
            document.getElementById("txt_username").onkeypress = function(event){
                if (event.keyCode == 13 || event.which == 13){
                    alert("You are clicked");
                }
            };
        </script>
    </head>
    <body>
         Username: <input type="text" id="txt_username" />
    </body>
</html>
Example.2 using onkeypress HTML Element Attribute
<html>
    <head>
        <script type="text/javascript">
            function doit_onkeypress(event){
                if (event.keyCode == 13 || event.which == 13){
                    alert("You are clicked");
                }
            }
        </script>
    </head>
    <body>
         Username: <input type="text" id="txt_username" onkeypress="javascript:doit_onkeypress(event);" />
    </body>
</html>
Demo & Output 
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details