Capturing The Enter Key Keypress in ASP.NET


/ Published in: JavaScript
Save to your folder(s)

This is to prevent a form from being inadvertently submitted.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. ////////////////////////////////
  3. // prototype.js is required
  4. ////////////////////////////////
  5.  
  6. var tbox = '<%= txtSearch.ClientID %>';
  7. var sbtn = '<%= btnSearch.ClientID %>';
  8.  
  9. Event.observe(window, 'load', init, false);
  10.  
  11. function init(){
  12. Event.observe(tbox, "keypress", function(e){checkEnter(e)});
  13. }
  14.  
  15. function checkEnter(e){
  16. if (e.keyCode == 13){
  17. $(sbtn).click();
  18. Event.stop(e);
  19. }
  20. }
  21. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.