onkeydown
Using jQuery to listen to keydown event
If you want to capture the keypress anywhere on the page – $(document).keypress(function(e) { if(e.which == 13) { // enter pressed } }); Don’t worry about the fact this checks for every keypress, it really isn’t putting any significant load on the browser.
How to capture a backspace on the onkeydown event
Try this: document.addEventListener(“keydown”, KeyCheck); //or however you are calling your method function KeyCheck(event) { var KeyID = event.keyCode; switch(KeyID) { case 8: alert(“backspace”); break; case 46: alert(“delete”); break; default: break; } }
Trigger a button click with JavaScript on the Enter key in a text box
In jQuery, the following would work: $(“#id_of_textbox”).keyup(function(event) { if (event.keyCode === 13) { $(“#id_of_button”).click(); } }); $(“#pw”).keyup(function(event) { if (event.keyCode === 13) { $(“#myButton”).click(); } }); $(“#myButton”).click(function() { alert(“Button code executed.”); }); <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> Username:<input id=”username” type=”text”><br> Password: <input id=”pw” type=”password”><br> <button id=”myButton”>Submit</button> Or in plain JavaScript, the following would work: document.getElementById(“id_of_textbox”) .addEventListener(“keyup”, function(event) { event.preventDefault(); … Read more