{"id":4805,"date":"2019-05-30T17:21:16","date_gmt":"2019-05-30T11:51:16","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4805"},"modified":"2026-10-30T12:15:40","modified_gmt":"2026-10-30T06:45:40","slug":"jquery-hide-and-show","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/jquery-hide-and-show\/","title":{"rendered":"jQuery Hide() and Show() Methods with Example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Post Date: May 30, 2019<br>Last Updated: October 30, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">jQuery is a JavaScript library that simplifies HTML document manipulation, event handling, animations, and other JavaScript operations. It provides simple methods to perform common tasks with less code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The jQuery <code>hide()<\/code> and <code>show()<\/code> methods are used to hide and display HTML elements dynamically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you can display an input field when a user switches a toggle button ON and hide it when the toggle button is switched OFF.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we will learn how to implement the jQuery <code>hide()<\/code> and <code>show()<\/code> methods using a Bootstrap toggle switch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before getting started, you should have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic knowledge of HTML.<\/li>\n\n\n\n<li>Basic understanding of JavaScript and jQuery.<\/li>\n\n\n\n<li>Familiarity with Bootstrap.<\/li>\n\n\n\n<li>A web browser to test the example.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Events<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">jQuery allows developers to execute JavaScript code in response to user interactions, such as clicking a button, moving the mouse over an element, or changing an input value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the <code>change()<\/code> event listens for changes in an input element. We can use this event to check whether a toggle switch is ON or OFF and display or hide another element accordingly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Include the Required Libraries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, include the required Bootstrap CSS, jQuery library, Bootstrap JavaScript, and Bootstrap Toggle CSS and JavaScript files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These libraries are used to create the toggle switch and handle its functionality.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the HTML Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a checkbox using the Bootstrap Toggle attributes. Add a separate <code>div<\/code> element containing the input field that needs to be displayed or hidden.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Implement the jQuery hide() and show() methods.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the jQuery <code>change()<\/code> event to detect the toggle switch state.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When the switch is ON, the <code>show()<\/code> method displays the input field.<\/li>\n\n\n\n<li>When the switch is OFF, the <code>hide()<\/code> method hides the input field.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following is the complete working HTML example.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\n    &lt;title>jQuery Hide and Show Example&lt;\/title>\n\n    &lt;!-- Bootstrap CSS -->\n    &lt;link rel=\"stylesheet\"\n          href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.4.0\/css\/bootstrap.min.css\">\n\n    &lt;!-- Bootstrap Toggle CSS -->\n    &lt;link rel=\"stylesheet\"\n          href=\"https:\/\/gitcdn.github.io\/bootstrap-toggle\/2.2.2\/css\/bootstrap-toggle.min.css\">\n&lt;\/head>\n\n&lt;body>\n\n    &lt;div class=\"container\">\n        &lt;h5>&lt;b>jQuery Hide() and Show()&lt;\/b>&lt;\/h5>\n\n        &lt;div class=\"row\">\n            &lt;div class=\"col-xs-12\">\n\n                &lt;p>Click here to enter ID:&lt;\/p>\n\n                &lt;!-- Toggle Switch -->\n                &lt;input type=\"checkbox\"\n                       id=\"chkPassport\"\n                       data-toggle=\"toggle\"\n                       data-on=\"ON\"\n                       data-off=\"OFF\">\n\n                &lt;!-- Input Field -->\n                &lt;div id=\"dvPassport\" style=\"display: none; margin-top: 15px;\">\n                    &lt;label for=\"passportId\">Enter ID:&lt;\/label>\n                    &lt;input type=\"text\"\n                           id=\"passportId\"\n                           class=\"form-control\"\n                           placeholder=\"Enter your ID\">\n                &lt;\/div>\n\n            &lt;\/div>\n        &lt;\/div>\n    &lt;\/div>\n\n    &lt;!-- jQuery -->\n    &lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.4.0\/jquery.min.js\">&lt;\/script>\n\n    &lt;!-- Bootstrap JavaScript -->\n    &lt;script src=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.4.0\/js\/bootstrap.min.js\">&lt;\/script>\n\n    &lt;!-- Bootstrap Toggle JavaScript -->\n    &lt;script src=\"https:\/\/gitcdn.github.io\/bootstrap-toggle\/2.2.2\/js\/bootstrap-toggle.min.js\">&lt;\/script>\n\n    &lt;!-- jQuery Hide and Show Functionality -->\n    &lt;script>\n        $(document).ready(function () {\n\n            $(\"#chkPassport\").change(function () {\n\n                if ($(this).is(\":checked\")) {\n                    $(\"#dvPassport\").show();\n                } else {\n                    $(\"#dvPassport\").hide();\n                }\n\n            });\n\n        });\n    &lt;\/script>\n\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Test the Functionality<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Save the code as <code>index.html<\/code> and open it in your web browser.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the toggle switch is turned ON, the ID input field will appear. When the switch is turned OFF, the input field will disappear.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-16.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"756\" height=\"197\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-16.png\" alt=\"jQuery hide and show example displaying a Bootstrap Toggle switch in the OFF state with the ID input field hidden.\" class=\"wp-image-12687\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-16.png 756w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-16-300x78.png 300w\" sizes=\"(max-width: 756px) 100vw, 756px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-17.png\"><img decoding=\"async\" width=\"760\" height=\"250\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-17.png\" alt=\"jQuery hide and show example displaying the ID input field when the Bootstrap Toggle switch is turned ON.\" class=\"wp-image-12688\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-17.png 760w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-17-300x99.png 300w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The jQuery <code>hide()<\/code> and <code>show()<\/code> methods make it easy to control the visibility of HTML elements based on user interactions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining the jQuery <code>change()<\/code> event with a Bootstrap Toggle switch, we can dynamically display or hide input fields without reloading the web page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These methods are useful when creating interactive forms, conditional input fields, and dynamic user interfaces.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">1. What is the difference between jQuery hide() and show()?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>hide()<\/code> method hides the selected HTML element, while the <code>show()<\/code> method makes the hidden element visible.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. How can I show an input field when a checkbox is selected?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the jQuery <code>change()<\/code> event to check whether the checkbox is selected. If it is checked, use <code>show()<\/code>; otherwise, use <code>hide()<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Can jQuery hide() and show() methods be used with Bootstrap?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. jQuery <code>hide()<\/code> and <code>show()<\/code> methods can be used with Bootstrap components to control the visibility of elements based on user interactions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/dynamic-column-header-result-ajax-call-jquery-datatable\/\">Dynamic Column Header Result Using AJAX Call in jQuery DataTable<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/selected-text-drop-list-select-box-using-jquery\/\">Selected Text Drop List Select Box Using jQuery<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Official Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/jquery.com\/\" target=\"_blank\" rel=\"noopener\">jQuery Official Website<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jquery.com\/hide\/\" target=\"_blank\" rel=\"noopener\">jQuery API Documentation \u2013 hide()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jquery.com\/show\/\" target=\"_blank\" rel=\"noopener\">jQuery API Documentation \u2013 show()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jquery.com\/change\/\" target=\"_blank\" rel=\"noopener\">jQuery API Documentation \u2013 change()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/minhur\/bootstrap-toggle\" target=\"_blank\" rel=\"noopener\">Bootstrap Toggle Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for help with web development, JavaScript, jQuery, or interactive website solutions? Our team can help you identify the right technology and build effective solutions for your business.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Contact our experts: <a href=\"https:\/\/pheonixsolutions.com\/contact\">Click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: May 30, 2019Last Updated: October 30, 2026 Introduction jQuery is a JavaScript library that simplifies HTML document manipulation, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12689,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[674],"class_list":["post-4805","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-architecture","tag-jquery-hide-and-show","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1fv","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/jQuery-Hide-and-Show-Tutorial.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4805"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4805\/revisions"}],"predecessor-version":[{"id":12690,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4805\/revisions\/12690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/12689"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}