{"id":851,"date":"2012-06-08T05:05:35","date_gmt":"2012-06-08T05:05:35","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=851"},"modified":"2012-06-08T05:05:35","modified_gmt":"2012-06-08T05:05:35","slug":"jquery-keycode-jquery-enter-keycode-jquery-escape-keycode","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-keycode-jquery-enter-keycode-jquery-escape-keycode\/","title":{"rendered":"jQuery Keycode Examples ( Enter button | Escape button )"},"content":{"rendered":"<p>Have you ever wanted to\u00a0use Enter and Escape button functionality for form \u00a0submission, etc.? In this article, I am going to discuss an easy way to do it using <em><strong>Enter Keycode and Escape Keycode in jQuery<\/strong><\/em>.<!--more--><\/p>\n<h2>Examples for Enter Keycode &amp; Escape Keycode using jQuery<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=851\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>When you run the following code, you will see the text &#8220;This is some text.&#8221;. Hit &#8220;Enter&#8221; on your keyboard and you will see the alert: &#8220;Enter button was pressed!&#8221;. If you hit Escape button on your\u00a0keyboard, you will see the alert: &#8220;Escape button was pressed!&#8221;.<\/p>\n<h3>Example 1: Using button keycodes in jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true crayon-selected\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;jQuery Keycode Examples for Enter button and Escape Button&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery_1.7.1_min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\nHit Enter on your keyboard.&lt;br\/&gt;\nHit Escape button on your keyboard.\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$(document).keyup(function(e) {\n\n\t\/\/Enter\n\tif (e.keyCode == 13)\n\t{\n\t\talert('Enter button was pressed!');\n\t}\n\n\t\/\/Escape\n\tif (e.keyCode == 27)\n\t{\n\t\talert('Ecape button was pressed!');\n\t}\n\n});\n\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h3>Example 2: Using button keycodes in jQuery<\/h3>\n<p>You can now extend this code for form submission or cancellation, like so:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).keyup(function(e) {\n\n\/\/Enter\n\nif (e.keyCode == 13)\n{\n\n$('#save').click();\n\n}\n\n\/\/Escape\n\nif (e.keyCode == 27)\n{\n\n$('#cancel').click();\n\n}\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>In the above example, #save refers to the ID of the Save button in a form and #cancel refers to the ID of the Cancel button in the same form. So now when you hit Enter \/ Cancel buttons, they perform the same action as your Save and Cancel buttons respectively as it simulates click event on those buttons.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=851\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other practical uses of <em><strong>Enter Keycode and Escape Keycode in jQuery<\/strong><\/em>? Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to\u00a0use Enter and Escape button functionality for form \u00a0submission, etc.? In this article, I am going to discuss an easy way to do it using Enter Keycode and Escape Keycode in jQuery.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[42,41],"class_list":["post-851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-dJ","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=851"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}