{"id":331,"date":"2013-10-24T22:28:44","date_gmt":"2013-10-24T14:28:44","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=331"},"modified":"2018-09-04T13:11:03","modified_gmt":"2018-09-04T05:11:03","slug":"how-to-use-jquery-to-detect-user-checking-and-unchecking-a-checkbox","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-detect-user-checking-and-unchecking-a-checkbox\/","title":{"rendered":"How to use jQuery to detect user checking and unchecking a checkbox"},"content":{"rendered":"<p>I wanted to show \/ hide some input fields when a user checks \/ unchecks a checkbox on a web page. <\/p>\n<p>To investigate how I can use jQuery to detect user checking and unchecking a checkbox, I cooked up a scenario.<\/p>\n<p>There is a page with a HTML checkbox and a message. When the checkbox is checked, the message shows; if not, the message hides.<\/p>\n<p>Pretty straightforward scenario to get me into working out a proof of concept to fulfill my objective. <\/p>\n<h2>Checking whether the checkbox is checked\/unchecked and showing\/hiding the message<\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nif ($('#messageVisibilityCheckbox').is(':checked')) {\r\n\t$('#messageContainer').hide();\r\n}\t\r\nelse {\r\n\t$('#messageContainer').show();\r\n}\r\n<\/pre>\n<p>To know whether the checkbox with id <code>messageVisibilityCheckbox<\/code> is check, I queries it by the id and calls <code>is(':checked')<\/code>. <\/p>\n<p>If the checkbox is checked, I queries the HTML element with <code>messageContainer<\/code> as its id and <code>calls<\/code> the hide function; if checkbox is not checked, I calls the <code>show<\/code> function. <\/p>\n<h2>Detecting user clicks on the HTML checkbox using jQuery<\/h2>\n<p>There are two approaches which I can use for detecting the event when the user clicks on the checkbox.<\/p>\n<h3>1. Registering a callback function to detect user clicks on the HTML checkbox<\/h3>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$('#messageVisibilityCheckbox').click(\r\n\tfunction () {\r\n\t\tif ($('#messageVisibilityCheckbox').is(':checked')) {\r\n\t\t\t$('#messageContainer').hide();\r\n\t\t}\t\r\n\t\telse {\r\n\t\t\t$('#messageContainer').show();\r\n\t\t}\r\n});\r\n<\/pre>\n<p>I queries the HTML checkbox with id <code>messageVisibilityCheckbox<\/code>, calls the <code>click<\/code> function and provides callback function containing the same code segment discussed above.<\/p>\n<h3>2. Registering a callback function to detect value changes on the HTML checkbox<\/h3>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$('#messageVisibilityCheckbox').change(\r\n\tfunction () {\r\n\t\tif ($('#messageVisibilityCheckbox').is(':checked')) {\r\n\t\t\t$('#messageContainer').hide();\r\n\t\t}\t\r\n\t\telse {\r\n\t\t\t$('#messageContainer').show();\r\n\t\t}\r\n});\r\n<\/pre>\n<p>I queries the HTML checkbox with id <code>messageVisibilityCheckbox<\/code>, calls the <code>change<\/code> function and provides a callback function containing the same code segment discussed above.<\/p>\n<h2>The HTML document for the checkbox, message and the user interaction logic<\/h2>\n<p>Referencing <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">my HTML reference<\/a> and including the JavaScript codes which I had created, I got the following HTML to verify my investigations:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n\t&lt;head&gt;\t\r\n\t\t&lt;title&gt;How to use jQuery to detect user checking and unchecking a checkbox&lt;\/title&gt;\r\n\t\t&lt;script src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n\t\t&lt;script type=&quot;text\/javascript&quot;&gt;\t\r\n\t\t$(document).ready(function() {\r\n\t\t\t$('#messageVisibilityCheckbox').change(\r\n\t\t\t\tfunction () {\r\n\t\t\t\t\tif ($('#messageVisibilityCheckbox').is(':checked')) {\r\n\t\t\t\t\t\t$('#messageContainer').hide();\r\n\t\t\t\t\t}\t\r\n\t\t\t\t\telse {\r\n\t\t\t\t\t\t$('#messageContainer').show();\r\n\t\t\t\t\t}\r\n\t\t\t});\r\n\t\t});\t\r\n\t\t&lt;\/script&gt;\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;form&gt;\r\n\t\t\t&lt;input type=&quot;checkbox&quot; id=&quot;messageVisibilityCheckbox&quot; name=&quot;messageVisibilityCheckbox&quot;\/&gt;\r\n\t\t\t&lt;label for=&quot;messageVisibilityCheckbox&quot;&gt;Hide the message below&lt;\/label&gt;\r\n\t\t&lt;\/form&gt;\r\n\t\t&lt;div id=&quot;messageContainer&quot;&gt;\r\n\t\t\tI am not hidden.\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div class=\"callToAction\">\n<a href=\"http:\/\/www.techcoil.com\/proof-of-concepts\/using-jquery-to-detect-user-checking-and-unchecking-a-checkbox\" class=\"readMoreLink\">See how this proof of concept works<\/a>\n<\/div>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-5l\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5l&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5l&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F331&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I wanted to show \/ hide some input fields when a user checks \/ unchecks a checkbox on a web page. <\/p>\n<p>To investigate how I can use jQuery to detect user checking and unchecking a checkbox, I cooked up a scenario.<\/p>\n<p>There is a page with a HTML checkbox and a message. When the checkbox is checked, the message shows; if not, the message hides.<\/p>\n<p>Pretty straightforward scenario to get me into working out a proof of concept to fulfill my objective. <\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[16,89,171],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5l","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/331"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=331"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/331\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}